mysql数据库模拟条件索引

  次阅读 来源:互联网(转载协议) 2016-01-27 15:43 我要评论(0)

我们知道,MySQL 不支持条件索引。 什么是条件索引呢? 条件索引就是在索引列上根据WHERE条件进行一定的过滤后产生的索引。 这样的索引有以下优势:

<strong>Table "ytt.girl1"

Column |Type|Modifiers

--------+---------+--------------------

id| integer | not null

rank| integer | not null default 0

Indexes:

"girl1_pkey" PRIMARY KEY, btree (id)

"idx_girl1_rank" btree (rank) WHERE rank >= 10 AND rank <= 100

执行的查询语句为:

select * from girl1 where rank between 20 and 60 limit 20;

用了全部索引的查询计划:

QUERY PLAN

---------------------------------------------------------------------------------------------------------------------------------

Limit(cost=0.29..36.58 rows=20 width=8) (actual time=0.024..0.054 rows=20 loops=1)

->Index Scan using idx_girl1_rank on girl1(cost=0.29..421.26 rows=232 width=8) (actual time=0.023..0.044 rows=20 loops=1)

Index Cond: ((rank >= 20) AND (rank <= 60))

Total runtime: 0.087 ms

(4 rows)

Time: 1.881 ms

用了条件索引的查询计划:

QUERY PLAN

---------------------------------------------------------------------------------------------------------------------------------

Limit(cost=0.28..35.54 rows=20 width=8) (actual time=0.036..0.068 rows=20 loops=1)

->Index Scan using idx_girl1_rank on girl1(cost=0.28..513.44 rows=291 width=8) (actual time=0.033..0.061 rows=20 loops=1)

Index Cond: ((rank >= 20) AND (rank <= 60))

Total runtime: 0.106 ms

(4 rows)

Time: 0.846 ms</strong>

<strong>ytt>show create table girl1_filtered_index; +----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | girl1_filtered_index | CREATE TABLE `girl1_filtered_index` ( `id` int(11) NOT NULL, `rank` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `idx_rank` (`rank`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +----------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) 接下来,对基础表的更新操作做下修改,创建了三个触发器。 DELIMITER $$ USE `t_girl`$$ DROP TRIGGER /*!50032 IF EXISTS */ `filtered_insert`$$ CREATE /*!50017 DEFINER = 'root'@'localhost' */ TRIGGER `filtered_insert` AFTER INSERT ON `girl1` FOR EACH ROW BEGIN IF new.rank BETWEEN 10 AND 100 THEN INSERT INTO girl1_filtered_index VALUES (new.id,new.rank); END IF; END; $$ DELIMITER ; DELIMITER $$ USE `t_girl`$$ DROP TRIGGER /*!50032 IF EXISTS */ `filtered_update`$$ CREATE /*!50017 DEFINER = 'root'@'localhost' */ TRIGGER `filtered_update` AFTER UPDATE ON `girl1` FOR EACH ROW BEGIN IF new.rank BETWEEN 10 AND 100 THEN REPLACE girl1_filtered_index VALUES (new.id,new.rank); ELSE DELETE FROM girl1_filtered_index WHERE id = old.id; END IF; END; $$ DELIMITER ; DELIMITER $$ USE `t_girl`$$ DROP TRIGGER /*!50032 IF EXISTS */ `filtered_delete`$$ CREATE /*!50017 DEFINER = 'root'@'localhost' */ TRIGGER `filtered_delete` AFTER DELETE ON `girl1` FOR EACH ROW BEGIN DELETE FROM girl1_filtered_index WHERE id = old.id; END; $$ DELIMITER ; OK,我们导入测试数据。 ytt>load data infile 'girl1.txt' into table girl1 fields terminated by ','; Query OK, 100000 rows affected (1.05 sec) Records: 100000 Deleted: 0 Skipped: 0 Warnings: 0 ytt>select count(*) from girl1; +----------+ | count(*) | +----------+ | 100000 | +----------+ 1 row in set (0.04 sec) ytt>select count(*) from girl1_filtered_index; +----------+ | count(*) | +----------+ | 640 | +----------+ 1 row in set (0.00 sec)</strong>

本站部分文章来源于网络以及网友投稿,本站只负责对文章进行整理、排版、编辑,是出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。如果您有什么意见或建议,请联系QQ28-1688-302!

人工智能实验室
相关文章相关文章
  • 第一批国家重点研发计划公布,旷视科技开启“五年行动”

    第一批国家重点研发计划公布,旷视科技开启“五年行动”

  • 让AI触手可及  Qualcomm携手创通联达推出全新终端侧AI开发套件

    让AI触手可及 Qualcomm携手创通联达推出全新终端侧AI开发套件

  • 品友互动为Digital Travel APAC2018 唯一受邀中国AI企业

    品友互动为Digital Travel APAC2018 唯一受邀中国AI企业

  • 长虹新款智能语音空调,告诉你空调也能玩Siri

    长虹新款智能语音空调,告诉你空调也能玩Siri

网友点评网友点评
阅读推荐阅读推荐

据外媒报道,STEER打造了首款完全自动驾驶停车技术,旨在使常规车辆转变为无人驾驶车辆。STEER的首款技术应用是4级自动驾驶及网络安全停车...

近日,美国软性机器抓手制造商 Soft Robotics 宣布,获得 2000 万美元的融资,本轮投资者包括 Scale Venture Partners,Calibrate Ventures...

据外媒报道,加州车管局发布了《2017自动驾驶脱离报告(California Autonomous Vehicle Disengagement Reports)》,其中谈及了脱离的具体...

用人机语音交互,来解决智能家居适老的问题;通过家庭门禁与安防套件、空气净化套件、可燃气体与有害气体监控套件等相互联动,在不同生活情...