mysql语句执行效率的优化问题
我现在有一个百万行的数据库,里面有相应规模的数据表,但是可能每次查询更新的行数不会超过20000行,因为用到了联表查询,特别的慢,有什么办法可以优化一下吗?
我虽然在where语句里加了筛选条件,但是仿佛联表的时候还是把全部的联上了
问题点数:50、回复次数:6Top
1 楼lobatt(网络爬虫)回复于 2006-03-20 16:47:27 得分 0
ps:我的数据库是mysql4.1 是为了能支持更新中的嵌套查询特地升级的,仿佛效率比3.2低好多Top
2 楼hy2003fly()回复于 2006-03-20 18:34:49 得分 20
EXPLAIN sql执行语句 分析一下为什么为这么慢
mysql> SELECT COUNT(*) FROM Headline WHERE ExpireTime >= 1112201600;
+----------+
| COUNT(*) |
+----------+
| 3971 |
+----------+
1 row in set (1.04 sec)
mysql> EXPLAIN SELECT COUNT(*) FROM Headline WHERE ExpireTime >= 1112201600 \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: Headline
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 302116
Extra: Using where
1 row in set (0.00 sec)
The NULL value in the key column of the EXPLAIN output tell us that MySQL won't be using an index for this query. In fact, the NULL value in the possible_keys column tells us that there were no indexes to pick from at all. If this type of query is likely to be common, we can simply add an index and rerun the query (or the EXPLAIN) to verify that MySQL uses it.
Top
3 楼da21(遇见)回复于 2006-03-20 18:46:59 得分 10
具体语句具体分析!
能给出语句看看吗?Top
4 楼lobatt(网络爬虫)回复于 2006-03-20 21:04:54 得分 0
是这样的,我的数据库里有1400000记录,我把id号大于1380000的记录项的某一列值置为0
例如:
update foo set bar=0 where id >=1380000
这样一条语句需要花上50多秒
而我实际的程序中还需要联表查询...
比如说
update foo left join foo1 on foo.id = foo1.id set bar = 0 where bar = 1 and foo1.bar1=0
仿佛联表的时候并没有先考虑where中的条件,所以特别慢...
原来我自己写了一个程序,每1000条记录更新一次,但是这样就要先把记录取出来
执行的语句也要多很多...所以升级到4.1,想用嵌套查询来节省时间,但是实际效果并不为我想像得那么好
虽然小规模数据的时候看起来更快,但是到了大规模之后却仿佛更慢了
而且慢得不可忍受...Top
5 楼XqYuan()回复于 2006-03-20 21:14:22 得分 20
索引Top
6 楼XqYuan()回复于 2006-03-20 21:15:19 得分 0
用到order by和做为条件的部分建索引Top




