加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

海量、多维数据让人抓狂?高效搜索方法看这里

发布时间:2019-09-17 07:40:54 所属栏目:MySql教程 来源:读芯术
导读:人与世界万物的互动会产生大量的时空数据。那么,当我们需要随时调用过去的数据时,改怎么办?尤其是面对各种海量、多维度的数据库,如果没有高效的搜索方法,我们只能望洋兴叹、束手无策。 别担心,本文将用详细的代码,手把手来传授高效搜索法的绝技! 对

直接编写SQL查询,而不是使用多CTID扫描。

  1. postgres=# explain (analyze,verbose,timing,costs,buffers) select * from tbl  
  2.  where  
  3.  crt_time between 2017-07-22 17:59:34 and 2017-07-22 17:59:40  
  4.  and (  
  5.  c1 in (1,2,3,4,100,200,99,88,77,66,55)  
  6.  or  
  7.  c2 < 10  
  8.  )  
  9.  and  
  10.  pos <-> point (0,0) < 5;  
  11.  Bitmap Heap Scan on postgres.tbl (cost=35022.06..44857.06 rows=2525 width=73) (actual time=205.542..214.547 rows=13 loops=1)  
  12.  Output: id, info, crt_time, pos, c1, c2, c3  
  13.  Recheck Cond: (((tbl.c1 = ANY ( {1,2,3,4,100,200,99,88,77,66,55} ::integer[])) OR (tbl.c2 < 10)) AND (tbl.crt_time >= 2017-07-22 17:59:34 ::timestamp without time zone) AND (tbl.crt_time <= 2017-07-22 17:59:40 ::timestamp without time zone))  
  14.  Filter: ((tbl.pos <-> (0,0) ::point) < 5 ::double precision)  
  15.  Rows Removed by Filter: 7834  
  16.  Heap Blocks: exact=6983  
  17.  Buffers: shared hit=14343  
  18.  -> BitmapAnd (cost=35022.06..35022.06 rows=7581 width=0) (actual time=203.620..203.620 rows=0 loops=1)  
  19.  Buffers: shared hit=7360  
  20.  -> BitmapOr (cost=1618.58..1618.58 rows=153936 width=0) (actual time=71.660..71.660 rows=0 loops=1)  
  21.  Buffers: shared hit=141  
  22.  -> Bitmap Index Scan on idx_tbl_1 (cost=0.00..806.54 rows=54151 width=0) (actual time=14.861..14.861 rows=54907 loops=1)  
  23.  Index Cond: (tbl.c1 = ANY ( {1,2,3,4,100,200,99,88,77,66,55} ::integer[]))  
  24.  Buffers: shared hit=88  
  25.  -> Bitmap Index Scan on idx_tbl_1 (cost=0.00..810.79 rows=99785 width=0) (actual time=56.797..56.797 rows=95147 loops=1)  
  26.  Index Cond: (tbl.c2 < 10)  
  27.  Buffers: shared hit=53  
  28.  -> Bitmap Index Scan on idx_tbl_2 (cost=0.00..33402.60 rows=2462443 width=0) (actual time=125.255..125.255 rows=2640751 loops=1)  
  29.  Index Cond: ((tbl.crt_time >= 2017-07-22 17:59:34 ::timestamp without time zone) AND (tbl.crt_time <= 2017-07-22 17:59:40 ::timestamp without time zone))  
  30.  Buffers: shared hit=7219  
  31.  Planning time: 0.160 ms  
  32.  Execution time: 216.797 ms  
  33. (22 rows) 

性能如预期的那样好,之前解释过原因。KNN条件以外的条件已经将结果收敛到7,000条记录,因此没有必要使用包含KNN条件的索引。(即使使用KNN索引也需要195毫秒,因为有60,687条记录满足KNN条件。)

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读