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)
相关文章推荐
- win7下,memCache安装过程
- Redis客户端GUI工具:RedisClient
- SQL中 and or优先级问题
- SQL查询月份有多少天(根据变量)
- 一键排班软件开发用到的sql语句
- 数据库设计中的常用技巧
- 高性能MySQL 第六章
- jboss7 datasource 当数据库重启自动重新获取连接
- oracle分页
- 给查询出的SQL记录添加序号列
- 判断数据库是否是数字 isnumeric
- ubuntu15.04中安装mysql和mysql-workbench
- hibernate sql查询转换成VO返回list
- 数据库基本操作
- sql嵌套批量更新
- Xtrabackup快速搭建MySQL主从复制
- 常见SQL语句语法错误解决
- 数据库学习2
- Oracle报错:ORA-00845: MEMORY_TARGET not supported on this system
- Spark修炼之道(高级篇)——Spark源码阅读:第十三节 Spark SQL之SQLContext(一)