谢谢各位大虾的热情回复。做实验验证了,确实是这个问题。在我的实验当中,大概在13%以内的样子。超过了可能会使用不到索引了。
mysql> select (select count(*) from test where DELETED=1)/(select count(*) from test);
+-------------------------------------------------------------------------+
| (select count(*) from test where DELETED=1)/(select count(*) from test) |
+-------------------------------------------------------------------------+
| 0.1378 |
+-------------------------------------------------------------------------+
mysql> explain select count(*) from test where test=23705 or test.DELETED=1 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: ALL
possible_keys: RECEIVERID,DELETED
key: NULL
key_len: NULL
ref: NULL
rows: 24044
Extra: Using where
1 row in set (0.00 sec)
mysql> update test set DELETED=0 where DELETED=1 limit 200;
Query OK, 200 rows affected, 1 warning (0.01 sec)
Rows matched: 200 Changed: 200 Warnings: 0
mysql> select (select count(*) from test where DELETED=1)/(select count(*) from test);
+------------------------------------------------------------------------+
| (select count(*) from test where DELETED=1)/(select count(*) from test |
+------------------------------------------------------------------------+
| 0.1262 |
+------------------------------------------------------------------------+
1 row in set (0.02 sec)
mysql> explain select count(*) from test where test.RECEIVERID=23705 or test.DELETED=1 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: test
type: index_merge
possible_keys: RECEIVERID,DELETED
key: RECEIVERID,DELETED
key_len: 4,1
ref: NULL
rows: 4677
Extra: Using union(RECEIVERID,DELETED); Using where
1 row in set (0.00 sec)
再次谢谢各位。


blog:
http://hi.baidu.com/hexie007