Redian新闻
>
more HELP: how to make this sql more efficient?
avatar
c*o
2
i got a timeout from my asp page in one questionnaire. how can i improve it?
or mostly no other ways? the checking of "AnswerUsed" took a little bit
long.
thanx thanx thanx!!!!!
DECLARE @QuestionnaireID int
SET @QuestionnaireID=1029
SELECT
Questions.QuestionID,
Questions.QuestionText,
Answers.AnswerText,
Answers.CorrectAnswer,
Answers.PointValue AS AnswerPointValue,
CASE WHEN EXISTS
(
SELECT 1
FROM
Scores S
avatar
c*t
3
打回出重写

it?

【在 c****o 的大作中提到】
: i got a timeout from my asp page in one questionnaire. how can i improve it?
: or mostly no other ways? the checking of "AnswerUsed" took a little bit
: long.
: thanx thanx thanx!!!!!
: DECLARE @QuestionnaireID int
: SET @QuestionnaireID=1029
: SELECT
: Questions.QuestionID,
: Questions.QuestionText,
: Answers.AnswerText,

avatar
c*o
4
you should let me know why
what you want me to improve?

【在 c**t 的大作中提到】
: 打回出重写
:
: it?

avatar
w*r
5
Your query sucks, give this a try, you better have index on isdeleted/
deleteds on both questions and
answers table. EV table also needs index. Otherwise, it will suck anyway.
SELECT
Questions.QuestionID,
Questions.QuestionText,
Answers.AnswerText,
Answers.CorrectAnswer,
Answers.PointValue AS AnswerPointValue,
CASE WHEN S1.AnswerID ID NOT NULL
THEN 1
ELSE 0
END AS AnswerUsed
FROM Questions
INNER JOIN
Answers
ON
(Questions.QuestionID = Answers.QuestionID
AND ANSWE

【在 c****o 的大作中提到】
: i got a timeout from my asp page in one questionnaire. how can i improve it?
: or mostly no other ways? the checking of "AnswerUsed" took a little bit
: long.
: thanx thanx thanx!!!!!
: DECLARE @QuestionnaireID int
: SET @QuestionnaireID=1029
: SELECT
: Questions.QuestionID,
: Questions.QuestionText,
: Answers.AnswerText,

avatar
c*o
6
多谢指教!
讨论一下几个问题
1。你把where里的条件尽量放在了join里,其实我也式过,得到的exe plan 好像一样
,可能sql server自己会做这样的优化(?)
2。我们每个表基本都有isdeleted这样的东西,查询常用到。但index似乎只在key上有
, 我一个developer要求改变数据库结构恐怕不行。我不懂的问一下,这个index重要在
哪里?如果确实必要,我就去找manager问一下...
3。exsists为什么不好?你改过的sql我想要加distinct的,而distinct好像比较
expensive.更麻烦的是distinct有些东西不能比较,比如text类型




【在 w*r 的大作中提到】
: Your query sucks, give this a try, you better have index on isdeleted/
: deleteds on both questions and
: answers table. EV table also needs index. Otherwise, it will suck anyway.
: SELECT
: Questions.QuestionID,
: Questions.QuestionText,
: Answers.AnswerText,
: Answers.CorrectAnswer,
: Answers.PointValue AS AnswerPointValue,
: CASE WHEN S1.AnswerID ID NOT NULL

avatar
w*r
7
some server can optimize the push down predicate better than others,
in your query, I believe all tables listed are "tables" rather than
complicated
business views. In some ad-hoc environment, complicated BI view hide
business logic and the 3-nr model beneath, therefore, when you select from
it and build joins on it, the pushdown predicate may or may not works fine.
This is the reason I tried to get all condition as close as to the join
condition.
since you are testing NULL for several columns,

【在 c****o 的大作中提到】
: 多谢指教!
: 讨论一下几个问题
: 1。你把where里的条件尽量放在了join里,其实我也式过,得到的exe plan 好像一样
: ,可能sql server自己会做这样的优化(?)
: 2。我们每个表基本都有isdeleted这样的东西,查询常用到。但index似乎只在key上有
: , 我一个developer要求改变数据库结构恐怕不行。我不懂的问一下,这个index重要在
: 哪里?如果确实必要,我就去找manager问一下...
: 3。exsists为什么不好?你改过的sql我想要加distinct的,而distinct好像比较
: expensive.更麻烦的是distinct有些东西不能比较,比如text类型
:

avatar
z*y
8
Can you run the following script and send me the result...or just post here:
SET SHOWPLAN_ALL ON;
GO
your query here:
SET SHOWPLAN_ALL OFF;
GO
单从表面看, 可能是有两个或两个以上的hash join, 另外你可能有index scan。 这
都说不准。 你把结果贴出来, 大家给你搂搂。

it?

【在 c****o 的大作中提到】
: i got a timeout from my asp page in one questionnaire. how can i improve it?
: or mostly no other ways? the checking of "AnswerUsed" took a little bit
: long.
: thanx thanx thanx!!!!!
: DECLARE @QuestionnaireID int
: SET @QuestionnaireID=1029
: SELECT
: Questions.QuestionID,
: Questions.QuestionText,
: Answers.AnswerText,

avatar
j*n
9
try to use UNION ALL
avatar
c*o
10
thank you!
in fact this query is an abbreviated one from another query. but i include
the main skeleton.
the format will be very ugly if i post the exe plan directly. so i am using
some attachments.
planforthis.xls is for the query i posted
fullquery.sql is the original query
planfull.xls is the plan for the fullquery.

here:

【在 z***y 的大作中提到】
: Can you run the following script and send me the result...or just post here:
: SET SHOWPLAN_ALL ON;
: GO
: your query here:
: SET SHOWPLAN_ALL OFF;
: GO
: 单从表面看, 可能是有两个或两个以上的hash join, 另外你可能有index scan。 这
: 都说不准。 你把结果贴出来, 大家给你搂搂。
:
: it?

avatar
c*o
11
你们是不是经常要看这个plan?幸亏我不是专门做这个 不然怎么活呀。。。
index scan 和 hash join 都应该避免? 很难吧。。。
加index基本不大可能也不懂。对改这个query不是很乐观。。。可能最后结果会把丫的拆了返回结果

here:

【在 z***y 的大作中提到】
: Can you run the following script and send me the result...or just post here:
: SET SHOWPLAN_ALL ON;
: GO
: your query here:
: SET SHOWPLAN_ALL OFF;
: GO
: 单从表面看, 可能是有两个或两个以上的hash join, 另外你可能有index scan。 这
: 都说不准。 你把结果贴出来, 大家给你搂搂。
:
: it?

avatar
c*o
12
我只在table上操作,没有view的级别亚
我刚才看天书一样看了一下exe plan,好像是像你说的,where里的东西自己优化到
join里了。
至于exists好像也用join自己优化了,而且sql server不知怎么可以用到返回scalar的
东西,所以应该比自己用distinct好点。这个answerused字段是判断有没有evaluation
用过这个answer,所以当有若干evaluation用过这个answer的时候就会返回多次,所以
按你的该法需要distinct.
关于index的问题我得再研究研究。。

.

【在 w*r 的大作中提到】
: some server can optimize the push down predicate better than others,
: in your query, I believe all tables listed are "tables" rather than
: complicated
: business views. In some ad-hoc environment, complicated BI view hide
: business logic and the 3-nr model beneath, therefore, when you select from
: it and build joins on it, the pushdown predicate may or may not works fine.
: This is the reason I tried to get all condition as close as to the join
: condition.
: since you are testing NULL for several columns,

avatar
c*o
13
在哪儿用union all?thanks!

【在 j*****n 的大作中提到】
: try to use UNION ALL
avatar
z*y
14
给你回了。
这个得一步步来。如果方向对头,再进行下一步。

using

【在 c****o 的大作中提到】
: thank you!
: in fact this query is an abbreviated one from another query. but i include
: the main skeleton.
: the format will be very ugly if i post the exe plan directly. so i am using
: some attachments.
: planforthis.xls is for the query i posted
: fullquery.sql is the original query
: planfull.xls is the plan for the fullquery.
:
: here:

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。