为什么这条语句会导致堵塞?

zq1956 2003-01-08 11:29:23
我的程序在运行过程中(多用户),产生了堵塞,我在enterprise manager里面看到的堵塞语句是一条select语句,上的锁是针对页的共享锁,但是在sql query里我运行它,差不多只要一分钟,为什么这条语句会导致堵塞?
...全文
44 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
KnowLittle 2003-01-08
  • 打赏
  • 举报
回复
mark,
不懂,帮你up,等待高手,自己学习
tjan 2003-01-08
  • 打赏
  • 举报
回复
可以把你这个表的结构和查询语句贴出来吗?
KnowLittle 2003-01-08
  • 打赏
  • 举报
回复
已经很好的配置了。。。
zq1956 2003-01-08
  • 打赏
  • 举报
回复
而且,1分钟真的很慢吗??
到底一个查询持续多长叫慢呢?
还有,单独运行一条这样的语句与在多用户并发的情况下运行这条语句差距会很大吗?硬件配置如上
zq1956 2003-01-08
  • 打赏
  • 举报
回复
有没有因为其他原因而导致这条语句运行缓慢呢,不过server的配置如下:
cpu :4*500
mem:2G
硬盘:64G
网络带宽:100M
环境对语句运行速度的影响到底有多大呢???
KnowLittle 2003-01-08
  • 打赏
  • 举报
回复
好东西,我来学习。。。:)
pengdali 2003-01-08
  • 打赏
  • 举报
回复
也看看你的表设计:

1 逻辑数据库和表的设计
  数据库的逻辑设计、包括表与表之间的关系是优化关系型数据库性能的核心。一个好的逻辑数据库设计可以为优化数据库和应用程序打下良好的基础。

  标准化的数据库逻辑设计包括用多的、有相互关系的窄表来代替很多列的长数据表。下面是一些使用标准化表的一些好处。

A:由于表窄,因此可以使排序和建立索引更为迅速
B:由于多表,所以多镞的索引成为可能
C:更窄更紧凑的索引
D:每个表中可以有少一些的索引,因此可以提高insert update delete等的速度,因为这些操作在索引多的情况下会对系统性能产生很大的影响
E:更少的空值和更少的多余值,增加了数据库的紧凑性由于标准化,所以会增加了在获取数据时引用表的数目和其间的连接关系的复杂性。太多的表和复杂的连接关系会降低服务器的性能,因此在这两者之间需要综合考虑。
  定义具有相关关系的主键和外来键时应该注意的事项主要是:用于连接多表的主键和参考的键要有相同的数据类型。

  2 索引的设计
A:尽量避免表扫描
检查你的查询语句的where子句,因为这是优化器重要关注的地方。包含在where里面的每一列(column)都是可能的侯选索引,为能达到最优的性能,考虑在下面给出的例子:对于在where子句中给出了column1这个列。
下面的两个条件可以提高索引的优化查询性能!
第一:在表中的column1列上有一个单索引
第二:在表中有多索引,但是column1是第一个索引的列
避免定义多索引而column1是第二个或后面的索引,这样的索引不能优化服务器性能
例如:下面的例子用了pubs数据库。
SELECT au_id, au_lname, au_fname FROM authors
WHERE au_lname = ’White’
按下面几个列上建立的索引将会是对优化器有用的索引
?au_lname
?au_lname, au_fname
而在下面几个列上建立的索引将不会对优化器起到好的作用
?au_address
?au_fname, au_lname
考虑使用窄的索引在一个或两个列上,窄索引比多索引和复合索引更能有效。用窄的索引,在每一页上
将会有更多的行和更少的索引级别(相对与多索引和复合索引而言),这将推进系统性能。
对于多列索引,SQL Server维持一个在所有列的索引上的密度统计(用于联合)和在第一个索引上的
histogram(柱状图)统计。根据统计结果,如果在复合索引上的第一个索引很少被选择使用,那么优化器对很多查询请求将不会使用索引。
有用的索引会提高select语句的性能,包括insert,uodate,delete。
但是,由于改变一个表的内容,将会影响索引。每一个insert,update,delete语句将会使性能下降一些。实验表明,不要在一个单表上用大量的索引,不要在共享的列上(指在多表中用了参考约束)使用重叠的索引。
在某一列上检查唯一的数据的个数,比较它与表中数据的行数做一个比较。这就是数据的选择性,这比较结果将会帮助你决定是否将某一列作为侯选的索引列,如果需要,建哪一种索引。你可以用下面的查询语句返回某一列的不同值的数目。
select count(distinct cloumn_name) from table_name
假设column_name是一个10000行的表,则看column_name返回值来决定是否应该使用,及应该使用什么索引。
Unique values Index

5000 Nonclustered index
20 Clustered index
3 No index

镞索引和非镞索引的选择

<1:>镞索引是行的物理顺序和索引的顺序是一致的。页级,低层等索引的各个级别上都包含实际的数据页。一个表只能是有一个镞索引。由于update,delete语句要求相对多一些的读操作,因此镞索引常常能加速这样的操作。在至少有一个索引的表中,你应该有一个镞索引。
在下面的几个情况下,你可以考虑用镞索引:
例如: 某列包括的不同值的个数是有限的(但是不是极少的)
顾客表的州名列有50个左右的不同州名的缩写值,可以使用镞索引。
例如: 对返回一定范围内值的列可以使用镞索引,比如用between,>,>=,<,<=等等来对列进行操作的列上。
select * from sales where ord_date between ’5/1/93’ and ’6/1/93’
例如: 对查询时返回大量结果的列可以使用镞索引。
SELECT * FROM phonebook WHERE last_name = ’Smith’

当有大量的行正在被插入表中时,要避免在本表一个自然增长(例如,identity列)的列上建立镞索引。如果你建立了镞的索引,那么insert的性能就会大大降低。因为每一个插入的行必须到表的最后,表的最后一个数据页。
当一个数据正在被插入(这时这个数据页是被锁定的),所有的其他插入行必须等待直到当前的插入已经结束。
一个索引的叶级页中包括实际的数据页,并且在硬盘上的数据页的次序是跟镞索引的逻辑次序一样的。

<2:>一个非镞的索引就是行的物理次序与索引的次序是不同的。一个非镞索引的叶级包含了指向行数据页的指针。
在一个表中可以有多个非镞索引,你可以在以下几个情况下考虑使用非镞索引。
在有很多不同值的列上可以考虑使用非镞索引
例如:一个part_id列在一个part表中
select * from employee where emp_id = ’pcm9809f’
查询语句中用order by 子句的列上可以考虑使用镞索引

3 查询语句的设计

SQL Server优化器通过分析查询语句,自动对查询进行优化并决定最有效的执行方案。优化器分析查询语句来决定那个子句可以被优化,并针对可以被优化查询的子句来选择有用的索引。最后优化器比较所有可能的执行方案并选择最有效的一个方案出来。
在执行一个查询时,用一个where子句来限制必须处理的行数,除非完全需要,否则应该避免在一个表中无限制地读并处理所有的行。
例如下面的例子,
select qty from sales where stor_id=7131
是很有效的比下面这个无限制的查询
select qty from sales
避免给客户的最后数据选择返回大量的结果集。允许SQL Server运行满足它目的的函数限制结果集的大小是更有效的。
这能减少网络I/O并能提高多用户的相关并发时的应用程序性能。因为优化器关注的焦点就是where子句的查询,以利用有用的索引。在表中的每一个索引都可能成为包括在where子句中的侯选索引。为了最好的性能可以遵照下面的用于一个给定列column1的索引。
第一:在表中的column1列上有一个单索引
第二:在表中有多索引,但是column1是第一个索引的列不要在where子句中使用没有column1列索引的查询语句,并避免在where子句用一个多索引的非第一个索引的索引。
这时多索引是没有用的。
For example, given a multicolumn index on the au_lname, au_fname columns of the authors table in
the pubs database,
下面这个query语句利用了au_lname上的索引
SELECT au_id, au_lname, au_fname FROM authors
WHERE au_lname = ’White’
AND au_fname = ’Johnson’
SELECT au_id, au_lname, au_fname FROM authors
WHERE au_lname = ’White’
下面这个查询没有利用索引,因为他使用了多索引的非第一个索引的索引
SELECT au_id, au_lname, au_fname FROM authors
WHERE au_fname = ’Johnson’
pengdali 2003-01-08
  • 打赏
  • 举报
回复
你先优化一下语句:

测试过程:

环境:
PIII900 128M
win2000+sql server2000企业版
table name :t_data
CLUSTERED PRIMARY KEY :id
Rows:441000

语句1:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 60000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:390ms

语句2:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 100000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:4s

语句3:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 150000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:5s

语句4:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 200000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:6s

语句5:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:过了15m没有出来,中断

语句6:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 300000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:过了20m没有出来,中断

语句7:
select top 60000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:20s

语句8:
select top 100000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:21s

语句9:
select top 150000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:23s

语句10:
select top 200000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:24s

语句11:
select top 250000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:26s

语句12:
select top 300000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:28s

语句13:
select top 400000 id into #a from t_data order by id asc

create index ind_a_base_id on #a(id)

select * from t_data where
id in (select top 10 id from #a order by id desc)
order by id asc

时间:29s

语句14:
select * from t_data where
id in
(select top 10 id from t_data where id in
(select top 250000 id from t_data order by id asc)
order by id desc)
order by id asc

时间:过了25m没有出来,还没有中断
pengdali 2003-01-08
  • 打赏
  • 举报
回复
估计是表设计问题!
昵称被占用了 2003-01-08
  • 打赏
  • 举报
回复
呵呵,要一分钟?
提高速度是关键。

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧