程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> MySQL模擬條件索引

MySQL模擬條件索引

編輯:關於MYSQL數據庫

       我們知道,MySQL 不支持條件索引。 什麼是條件索引呢? 條件索引就是在索引列上根據WHERE條件進行一定的過濾後產生的索引。 這樣的索引有以下優勢:
    ?

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 <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> <strong>select a.id,a.rank from girl1 as a where a.id in (select b.id from girl1_filtered_index as b where b.rank between 20 and 60) limit 20;</strong>
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved