一个表里有400万条记录,查询时用了2分钟,怎么提高查询速度?
我用select count(*) from 表名,执行用了2分钟,select colo1 from 表名 where colo2=?执行也要1分多钟,请高手帮我找个解决办法 问题点数:20、回复次数:31Top
1 楼jiezhi(风满袖)回复于 2006-09-20 10:29:52 得分 0
合理地建立索引Top
2 楼footpath(footpath)回复于 2006-09-20 14:37:06 得分 0
create indexTop
3 楼lcc_2004(我爱程序开发)回复于 2006-09-20 15:22:47 得分 0
创建索引!!Top
4 楼dataopen(数据通科技)回复于 2006-09-21 10:22:37 得分 0
换数据库Top
5 楼Gucciwu(一意孤行 www.57studio.net)回复于 2006-09-22 11:13:00 得分 0
建index是正解,另外你用的什么数据库??Top
6 楼aimcy(Debian)回复于 2006-10-05 12:11:12 得分 0
PI不够合理
Top
7 楼SethDelphi(所有程序只写半天)回复于 2006-10-08 13:37:13 得分 0
1.select count(*) from 表名
SELECT rows
FROM sysindexes
WHERE id = OBJECT_ID('<table_name>') AND indid < 2
Assuming you have the database option turned on to "Auto Create Statistics" and "Auto Update Statistics",
2.select colo1 from 表名 where colo2=?
case colo2 is very high selective.
set index : let colo2 be number , best integer.
case colo2 only contains 1-10 value
Horizontal partitioning it.
else
redesinge relative tables
last resort
buy new powerful hardware.
Top
8 楼47522341(睡到8:30)回复于 2006-10-08 14:00:53 得分 0
每条记录有多大?
count(*) 的话肯定是要进行全表扫描,怕是建立索引也不好用。Top
9 楼47522341(睡到8:30)回复于 2006-10-08 14:02:04 得分 0
针对不同的应用建立索引吧。
如果非要count(*)
那就建议加一个identity字段。查找的时候找到这个最大值。这样可以将查找用时分散,速度会快些。Top
10 楼cobejordan(非菲非霏)回复于 2006-10-08 15:30:01 得分 0
硬件配置?
用的什么数据库?Top
11 楼Hopewell_Go(好的在后頭﹗希望更好﹗﹗)回复于 2006-10-09 20:19:51 得分 0
建立索引。。Top
12 楼littlechen(littlechen)回复于 2006-10-09 21:29:41 得分 0
如果是sql server 的话
对该表的某个字段进行索引,
select count(该字段) from 表名
应该会明显提高速度Top
13 楼aimcy(Debian)回复于 2006-10-23 10:14:08 得分 0
25773204条记录,用时19s
index很重要Top
14 楼hank212(IT民工)回复于 2006-10-23 10:17:47 得分 0
主键加索引Top
15 楼rock1124(带着上帝和女涡逛街)回复于 2006-10-24 17:05:57 得分 0
create indexTop
16 楼guoyaowu(guoyaowu)回复于 2006-10-26 10:30:11 得分 0
还是create index好啊Top
17 楼sunruping(孙茹苹)回复于 2006-10-26 14:18:09 得分 0
加自动增长列,建立索引就够了。你才400万,1400万的都没问题。Top
18 楼janglily210(风间苍月)回复于 2006-10-28 23:31:05 得分 0
if you are using oracle ,create index first then analyse tableTop
19 楼duochunyu()回复于 2006-10-30 17:08:47 得分 0
select count(1) from..Top
20 楼EmeraldSword(035216)回复于 2006-10-31 20:39:00 得分 0
合理建立索引!Top
21 楼lwb111(★★★)回复于 2006-11-02 15:46:03 得分 0
写sql语句的时候,不要用*号,手动把所有字段写上,写*号会很费时的。几百万的数据,就会很慢的。经理压缩一下数据库,清理一下日志等维护工作Top
22 楼jo_kingbird(jo_kingbird)回复于 2006-11-03 10:46:04 得分 0
如果是sql server 的话
对该表的某个字段进行索引,
select count(该字段) from 表名
应该会明显提高速度
**************************************
速度应该是一样的Top
23 楼allright_flash()回复于 2006-11-03 10:55:22 得分 0
数据量大的话,换oracle,查询速度快,
如果不换数据库,那就建索引吧,
优化你的sql语句,尽量少用子查询,in之类的。。。。
Top
24 楼qiuqiang82(海之鹞)回复于 2006-11-07 09:06:18 得分 0
create index 才是正解Top
25 楼jijiang1981(纪江)回复于 2006-11-07 16:46:30 得分 0
1.select count(*) from 表名
建索引也没有办法优化
sybase中可以用系统过程sp_spaceused取得记录数
2.select colo1 from 表名 where colo2=?执行也要1分多钟
如果在colo2没有建索引,那应该对其创建索引
如果在colo2上已建索引还比慢,就应遵循查询参数规则(SARGS)。如果还不行就要察看是否是数据库服务器瓶颈。
我这里从一亿条记录中利用索引查询10s中搞定。
Top
26 楼howesen(无名小卒)回复于 2006-11-14 11:28:05 得分 0
路过!Top
27 楼wang_h_t_(请大家多帮助)回复于 2006-11-15 11:25:21 得分 0
检查数据分布,PI是否合理,Index是否合理,能否达到并行.
和你用的数据库有很大关系>Top
28 楼studenthj()回复于 2006-11-15 16:53:07 得分 0
用索引Top
29 楼jiangchuandong(岁月的流逝......)回复于 2006-11-23 19:34:20 得分 0
分区表处理Top
30 楼studenthj()回复于 2006-11-23 20:11:36 得分 0
用索引(create index)Top
31 楼scorpiowell(年年岁岁花相似,岁岁年年人不同)回复于 2006-11-30 15:08:22 得分 0
在sql server中取某表的行数:
SELECT rows
FROM sysindexes
WHERE id = OBJECT_ID('tablename') AND indid < 2
select colo1 from 表名 where colo2=?
为colo1创建索引,如果colo1字段长度太长,并且没有它的数据没什么规律,建索引还是会很慢的,分区吧,将历史数据备份到别的区。Top




