mysql> explain select count(*) from `p1`,`p1gl` where `p1`.`id`=`p1gl`.`pid` and
`p1gl`.tid in(1,2,3);
+----+-------------+-------+--------+---------------+---------+---------+-------
--------+----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref
| rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-------
--------+----------+--------------------------+
| 1 | SIMPLE | p1gl | index | pid,tid,pid_2 | pid_2 | 8 | NULL
| 11167260 | Using where; Using index |
| 1 | SIMPLE | p1 | eq_ref | PRIMARY | PRIMARY | 4 | test.p
1gl.pid | 1 | Using index |
+----+-------------+-------+--------+---------------+---------+---------+-------
--------+----------+--------------------------+
2 rows in set (0.00 sec)
mysql> explain select count(*) from `p1`,`p1gl` where `p1`.`id`=`p1gl`.`pid` and
`p1gl`.tid in(1,2,3)\G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: p1gl
type: index
possible_keys: pid,tid,pid_2
key: pid_2
key_len: 8
ref: NULL
rows: 11167260
Extra: Using where; Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: p1
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: test.p1gl.pid
rows: 1
Extra: Using index
2 rows in set (0.00 sec)
mysql> select count(*) from `p1`,`p1gl` where `p1`.`id`=`p1gl`.`pid` and `p1gl`.
tid in(1,2,3);
+----------+
| count(*) |
+----------+
| 3350466 |
+----------+
1 row in set (15.14 sec)
Server version: 5.1.40-community MySQL Community Server (GPL)
select a.id,b.id from a,b where a.id <1000000 and a.id=b.id and a.tid in(1,3,4);
select a.id,b.id from a,b where 1000000 <a.id <2000000 and a.id=b.id and a.tid in(1,3,4);
... 就是把一个查询分开了,呵呵 不过如果数据库的表在创建时就分表的话就更好了作者: mmxcq 时间: 2010-03-08 23:50
回复 mmxcq
select a.id,b.id from a,b where a.id
taluyi 发表于 2010-03-08 17:22
select a.id,b.id from a,b where a.id
taluyi 发表于 2010-03-08 17:22
mysql> select count(*) from p1,p1gl where p1.id <1000000 and p1.id=p1gl.pid a
nd p1gl.tid in(1,3,4);
+----------+
| count(*) |
+----------+
| 300044 |
+----------+
1 row in set (1.33 sec)
mysql> select count(*) from p1,p1gl where 1000000 <p1.id <2000000 and p1.id
=p1gl.pid and p1gl.tid in(1,3,4);
+----------+
| count(*) |
+----------+
| 3350340 |
+----------+
1 row in set (27.61 sec)
mysql> select count(*) from p1,p1gl where p1.id>1000000 and p1.id=p1gl.pi
d and p1gl.tid in(1,3,4);
+----------+
| count(*) |
+----------+
| 3050295 |
+----------+
1 row in set (13.33 sec)