您的位置:首页 > 数据库

sql 字段先计算后再拿比对的字段进行比对 效率提升100倍

2015-12-30 09:47 465 查看
关于日期索引的使用,不要计算后再对比,否则使用不了索引
例如:以下执行不了索引,耗时很大

dywl=# explain analyze SELECT
car_bill.billno,car_bill.beginunit,car_bill.begincity,car_bill.endunit,car_bill.endcity,car_bill.pubtime,car_bill.goodstype,car_bill.mobile,
ST_Distance_Sphere(car_bill.point_geom,ST_GeometryFromText('POINT(113.91269 35.307258)',4326)) as leng,
sys_user.name,car_bill.totaltone,car_bill.weight,car_bill.carriage,car_bill.carriageunit,car_bill.remark
FROM
car_bill,sys_user
WHERE
EXTRACT(EPOCH FROM now()- car_bill.pubtime) < 60
and
car_bill.userid=sys_user.userid
and
ST_Distance_Sphere(car_bill.point_geom,ST_GeometryFromText('POINT(113.91269 35.307258)',4326))<=200000
ORDER BY
car_bill.point_geom <-> ST_GeometryFromText('POINT(113.91269 35.307258)',4326)    ;

QUERY PLAN

------------------------------------------------------------------
Sort  (cost=258916.19..259035.52 rows=47734 width=146) (actual time=565.754..565.754 rows=0 loops=1)
Sort Key: ((car_bill.point_geom <-> '0101000020E6100000B8585183697A5C4099B7EA3A54A74140'::geometry))
Sort Method: quicksort  Memory: 25kB
->  Hash Join  (cost=59228.31..251615.60 rows=47734 width=146) (actual time=565.745..565.745 rows=0 loops=1)
Hash Cond: ((car_bill.userid)::text = (sys_user.userid)::text)
->  Seq Scan on car_bill  (cost=0.00..173902.11 rows=47734 width=153) (actual time=565.743..565.743 rows=0 loops=1)
Filter: ((date_part('epoch'::text, (now() - (pubtime)::timestamp with time zone)) < 60::double precision) AND (_st_distance(geography(point_geom), '0101000020E6100000B8585183
697A5C4099B7EA3A54A74140'::geography, 0::double precision, false) <= 200000::double precision))
Rows Removed by Filter: 423616
->  Hash  (cost=49538.47..49538.47 rows=527747 width=17) (never executed)
->  Seq Scan on sys_user  (cost=0.00..49538.47 rows=527747 width=17) (never executed)
Planning time: 1.064 ms
Execution time: 565.836 ms
(12 rows)


变更为以下后,可走索引,提升百倍,所以下面的才是正确的

dywl=#  explain analyze SELECT
dywl-#                           car_bill.billno,car_bill.beginunit,car_bill.begincity,car_bill.endunit,car_bill.endcity,car_bill.pubtime,car_bill.goodstype,car_bill.mobile,
dywl-#                           ST_Distance_Sphere(car_bill.point_geom,ST_GeometryFromText('POINT(113.91269 35.307258)',4326)) as leng,
dywl-#                           sys_user.name,car_bill.totaltone,car_bill.weight,car_bill.carriage,car_bill.carriageunit,car_bill.remark
dywl-#                       FROM
dywl-#                           car_bill,sys_user
dywl-#                       WHERE
dywl-#                           car_bill.pubtime > now() - interval '60 seconds'
dywl-#                           and car_bill.userid=sys_user.userid
dywl-#                           and ST_Distance_Sphere(car_bill.point_geom,ST_GeometryFromText('POINT(113.91269 35.307258)',4326))<=200000
dywl-#                       ORDER BY
dywl-#                           car_bill.point_geom <-> ST_GeometryFromText('POINT(113.91269 35.307258)',4326)
dywl-# ;

QUERY PLAN
----------------------------------------------------------------------------------------------------------
Sort  (cost=4424.85..4425.43 rows=235 width=146) (actual time=0.369..0.369 rows=0 loops=1)
Sort Key: ((car_bill.point_geom <-> '0101000020E6100000B8585183697A5C4099B7EA3A54A74140'::geometry))
Sort Method: quicksort  Memory: 25kB
->  Nested Loop  (cost=0.85..4415.59 rows=235 width=146) (actual time=0.351..0.351 rows=0 loops=1)
->  Index Scan using car_bill_pubtime_idx on car_bill  (cost=0.43..2388.74 rows=235 width=153) (actual time=0.351..0.351 rows=0 loops=1)
Index Cond: ((pubtime)::timestamp without time zone > (now() - '00:01:00'::interval))
Filter: (_st_distance(geography(point_geom), '0101000020E6100000B8585183697A5C4099B7EA3A54A74140'::geography, 0::double precision, false) <= 200000::double precision)
Rows Removed by Filter: 1
->  Index Scan using sys_user_userid on sys_user  (cost=0.42..8.36 rows=1 width=17) (never executed)
Index Cond: ((userid)::text = (car_bill.userid)::text)
Planning time: 1.161 ms
Execution time: 0.483 ms
(12 rows)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: