Hi
Where can I find good resources re: peformance tuning of queries / indexes ,
using EXPLAIN etc ?
Thanks
BruceBecause you have EXPLAIN in capitals I am going to presume you either
1. Want the SQL Server equivalent to Oracle's EXPLAIN PLAN
2. Have the wrong Newsgroup
Presuming #1 you can look at.
SET SHOWPLAN_ALL ON
once you have that then you can start to look to articles like this
http://www.sql-server-performance.com/query_execution_plan_analysis.asp
or anything on the subject by Kalen Delaney.
--
--
Allan Mitchell (Microsoft SQL Server MVP)
MCSE,MCDBA
www.SQLDTS.com
I support PASS - the definitive, global community
for SQL Server professionals - http://www.sqlpass.org
"Bruce Baker" <bruceb@.ardex.com.au> wrote in message
news:eWvSyqJuDHA.2448@.TK2MSFTNGP12.phx.gbl...
> Hi
> Where can I find good resources re: peformance tuning of queries / indexes
,
> using EXPLAIN etc ?
> Thanks
> Bruce
>|||www.sql-server-performance.com will be a good resource.
Suresh
>--Original Message--
>Because you have EXPLAIN in capitals I am going to
presume you either
>1. Want the SQL Server equivalent to Oracle's EXPLAIN
PLAN
>2. Have the wrong Newsgroup
>
>Presuming #1 you can look at.
>SET SHOWPLAN_ALL ON
>once you have that then you can start to look to articles
like this
>http://www.sql-server-
performance.com/query_execution_plan_analysis.asp
>or anything on the subject by Kalen Delaney.
>--
>--
>Allan Mitchell (Microsoft SQL Server MVP)
>MCSE,MCDBA
>www.SQLDTS.com
>I support PASS - the definitive, global community
>for SQL Server professionals - http://www.sqlpass.org
>"Bruce Baker" <bruceb@.ardex.com.au> wrote in message
>news:eWvSyqJuDHA.2448@.TK2MSFTNGP12.phx.gbl...
>> Hi
>> Where can I find good resources re: peformance tuning
of queries / indexes
>,
>> using EXPLAIN etc ?
>> Thanks
>> Bruce
>>
>
>.
>
Showing posts with label indexes. Show all posts
Showing posts with label indexes. Show all posts
Friday, March 23, 2012
query performance tuning
Wednesday, March 21, 2012
Query Performance and Index
Dear Forum,
Please help me modify this query for optimum performance and suggest indexes
to create. Thanks. James
SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition,
BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinitionHi James
I added parenthesis to your query:
SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition,
BBNew.dbo.fffCases.StateFips,
MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
FROM
BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
AND
BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
(BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
GROUP BY
BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
Please let me know if this is correct.
If it is correct, then there is no need of changing the query, your query
looks perfect.
There might be Primary Keys declared on the tables that are used here.
Else create Index on the columns that are involved in the where clause.
please let me know your comments
thanks and regards
Chandra
"James Juno" wrote:
> Dear Forum,
> Please help me modify this query for optimum performance and suggest indexes
> to create. Thanks. James
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition
>|||Adding to my previous post, what i feel is, the first OR should be replace
with AND:
SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition,
BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode =BBNew.dbo.fffCourtCodes.MST_COURT_CODE
AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode AND
BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition
"Chandra" wrote:
> Hi James
> I added parenthesis to your query:
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,
> MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND
> BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> GROUP BY
> BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> Please let me know if this is correct.
> If it is correct, then there is no need of changing the query, your query
> looks perfect.
> There might be Primary Keys declared on the tables that are used here.
> Else create Index on the columns that are involved in the where clause.
> please let me know your comments
> thanks and regards
> Chandra
> "James Juno" wrote:
> > Dear Forum,
> >
> > Please help me modify this query for optimum performance and suggest indexes
> > to create. Thanks. James
> >
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition
> >|||Chandra,
This is great. Thank you so much. I haven't tried it yet - but it looks good.
James.
"Chandra" wrote:
> Hi James
> I added parenthesis to your query:
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,
> MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND
> BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> GROUP BY
> BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> Please let me know if this is correct.
> If it is correct, then there is no need of changing the query, your query
> looks perfect.
> There might be Primary Keys declared on the tables that are used here.
> Else create Index on the columns that are involved in the where clause.
> please let me know your comments
> thanks and regards
> Chandra
> "James Juno" wrote:
> > Dear Forum,
> >
> > Please help me modify this query for optimum performance and suggest indexes
> > to create. Thanks. James
> >
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition
> >|||To avoid confusion, use ansi join instead old style. Also use alias to make
the statement more readable.
SELECT
cc.MST_COURT_NAME,
ct.CTdefinition,
rt.RTdefinition,
c.StateFips,
MIN(substring(c.JudgmentDate, 1,6)),
MAX(substring(c.JudgmentDate, 1,6))
FROM
BBNew.dbo.fffCases as c
inner join
BBNew.dbo.fffCourtCodes as cc
on c.CourtCode = cc.MST_COURT_CODE
inner join
BBNew.dbo.fffCourtTypes as ct
on c.CourtType = ct.CTcode
inner join
BBNew.dbo.fffRecordTypes as rt
on c.FilingType = rt.RTcode
WHERE
c.StateFips LIKE :Param_State
OR c.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
GROUP BY
c.StateFips,
cc.MST_COURT_NAME,
ct.CTdefinition,
rt.RTdefinition;
Be sure to have an index by:
- BBNew.dbo.fffCases.PostedDate <-- clustered one
- BBNew.dbo.fffCases.StateFips
- BBNew.dbo.fffCases.CourtCode
- BBNew.dbo.fffCases.CourtType
- BBNew.dbo.fffCases.FilingType
- BBNew.dbo.fffCourtCodes.MST_COURT_CODE
- BBNew.dbo.fffCourtTypes.CTcode
- BBNew.dbo.fffRecordTypes.rt.RTcode
AMB
"James Juno" wrote:
> Chandra,
> This is great. Thank you so much. I haven't tried it yet - but it looks good.
> James.
> "Chandra" wrote:
> > Hi James
> > I added parenthesis to your query:
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,
> > MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> > MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> >
> > FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> > BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> >
> > WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND
> > BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> >
> > (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> > OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> > OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> >
> > GROUP BY
> > BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> >
> > Please let me know if this is correct.
> >
> > If it is correct, then there is no need of changing the query, your query
> > looks perfect.
> > There might be Primary Keys declared on the tables that are used here.
> >
> > Else create Index on the columns that are involved in the where clause.
> >
> > please let me know your comments
> >
> > thanks and regards
> > Chandra
> >
> > "James Juno" wrote:
> >
> > > Dear Forum,
> > >
> > > Please help me modify this query for optimum performance and suggest indexes
> > > to create. Thanks. James
> > >
> > > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > BBNew.dbo.fffRecordTypes.RTdefinition,
> > > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > BBNew.dbo.fffRecordTypes.RTdefinition
> > >|||Alejandro,
Great. Thank you.
James
"Alejandro Mesa" wrote:
> To avoid confusion, use ansi join instead old style. Also use alias to make
> the statement more readable.
> SELECT
> cc.MST_COURT_NAME,
> ct.CTdefinition,
> rt.RTdefinition,
> c.StateFips,
> MIN(substring(c.JudgmentDate, 1,6)),
> MAX(substring(c.JudgmentDate, 1,6))
> FROM
> BBNew.dbo.fffCases as c
> inner join
> BBNew.dbo.fffCourtCodes as cc
> on c.CourtCode = cc.MST_COURT_CODE
> inner join
> BBNew.dbo.fffCourtTypes as ct
> on c.CourtType = ct.CTcode
> inner join
> BBNew.dbo.fffRecordTypes as rt
> on c.FilingType = rt.RTcode
> WHERE
> c.StateFips LIKE :Param_State
> OR c.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> GROUP BY
> c.StateFips,
> cc.MST_COURT_NAME,
> ct.CTdefinition,
> rt.RTdefinition;
> Be sure to have an index by:
> - BBNew.dbo.fffCases.PostedDate <-- clustered one
> - BBNew.dbo.fffCases.StateFips
> - BBNew.dbo.fffCases.CourtCode
> - BBNew.dbo.fffCases.CourtType
> - BBNew.dbo.fffCases.FilingType
> - BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> - BBNew.dbo.fffCourtTypes.CTcode
> - BBNew.dbo.fffRecordTypes.rt.RTcode
>
> AMB
>
> "James Juno" wrote:
> > Chandra,
> >
> > This is great. Thank you so much. I haven't tried it yet - but it looks good.
> >
> > James.
> >
> > "Chandra" wrote:
> >
> > > Hi James
> > > I added parenthesis to your query:
> > > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > BBNew.dbo.fffRecordTypes.RTdefinition,
> > > BBNew.dbo.fffCases.StateFips,
> > > MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> > > MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> > >
> > > FROM
> > > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> > > BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> > >
> > > WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > > AND
> > > BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > >
> > > (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> > > OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> > > OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> > >
> > > GROUP BY
> > > BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> > >
> > > Please let me know if this is correct.
> > >
> > > If it is correct, then there is no need of changing the query, your query
> > > looks perfect.
> > > There might be Primary Keys declared on the tables that are used here.
> > >
> > > Else create Index on the columns that are involved in the where clause.
> > >
> > > please let me know your comments
> > >
> > > thanks and regards
> > > Chandra
> > >
> > > "James Juno" wrote:
> > >
> > > > Dear Forum,
> > > >
> > > > Please help me modify this query for optimum performance and suggest indexes
> > > > to create. Thanks. James
> > > >
> > > > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > > BBNew.dbo.fffRecordTypes.RTdefinition,
> > > > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > > > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > > > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > > > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > > > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > > > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > > > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > > > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > > > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > > > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > > BBNew.dbo.fffRecordTypes.RTdefinition
> > > >|||In general, it is a good practice to create a Primary Key constraint on
all tables (which will automatically create a unique index), and to
create indexes on foreign key constraints.
You did not post any DDL, so the keys and indexes cannot be reviewed.
But the query would benefit if all join columns were indexed.
Gert-Jan
James Juno wrote:
> Dear Forum,
> Please help me modify this query for optimum performance and suggest indexes
> to create. Thanks. James
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition|||Gert-Jan,
I got the script that works. Thanks for your contribution.
James
"Gert-Jan Strik" wrote:
> In general, it is a good practice to create a Primary Key constraint on
> all tables (which will automatically create a unique index), and to
> create indexes on foreign key constraints.
> You did not post any DDL, so the keys and indexes cannot be reviewed.
> But the query would benefit if all join columns were indexed.
> Gert-Jan
>
> James Juno wrote:
> >
> > Dear Forum,
> >
> > Please help me modify this query for optimum performance and suggest indexes
> > to create. Thanks. James
> >
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition
>
Please help me modify this query for optimum performance and suggest indexes
to create. Thanks. James
SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition,
BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinitionHi James
I added parenthesis to your query:
SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition,
BBNew.dbo.fffCases.StateFips,
MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
FROM
BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
AND
BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
(BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
GROUP BY
BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
Please let me know if this is correct.
If it is correct, then there is no need of changing the query, your query
looks perfect.
There might be Primary Keys declared on the tables that are used here.
Else create Index on the columns that are involved in the where clause.
please let me know your comments
thanks and regards
Chandra
"James Juno" wrote:
> Dear Forum,
> Please help me modify this query for optimum performance and suggest indexes
> to create. Thanks. James
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition
>|||Adding to my previous post, what i feel is, the first OR should be replace
with AND:
SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition,
BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode =BBNew.dbo.fffCourtCodes.MST_COURT_CODE
AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode AND
BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
BBNew.dbo.fffCourtTypes.CTdefinition,
BBNew.dbo.fffRecordTypes.RTdefinition
"Chandra" wrote:
> Hi James
> I added parenthesis to your query:
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,
> MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND
> BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> GROUP BY
> BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> Please let me know if this is correct.
> If it is correct, then there is no need of changing the query, your query
> looks perfect.
> There might be Primary Keys declared on the tables that are used here.
> Else create Index on the columns that are involved in the where clause.
> please let me know your comments
> thanks and regards
> Chandra
> "James Juno" wrote:
> > Dear Forum,
> >
> > Please help me modify this query for optimum performance and suggest indexes
> > to create. Thanks. James
> >
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition
> >|||Chandra,
This is great. Thank you so much. I haven't tried it yet - but it looks good.
James.
"Chandra" wrote:
> Hi James
> I added parenthesis to your query:
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,
> MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND
> BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> GROUP BY
> BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> Please let me know if this is correct.
> If it is correct, then there is no need of changing the query, your query
> looks perfect.
> There might be Primary Keys declared on the tables that are used here.
> Else create Index on the columns that are involved in the where clause.
> please let me know your comments
> thanks and regards
> Chandra
> "James Juno" wrote:
> > Dear Forum,
> >
> > Please help me modify this query for optimum performance and suggest indexes
> > to create. Thanks. James
> >
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition
> >|||To avoid confusion, use ansi join instead old style. Also use alias to make
the statement more readable.
SELECT
cc.MST_COURT_NAME,
ct.CTdefinition,
rt.RTdefinition,
c.StateFips,
MIN(substring(c.JudgmentDate, 1,6)),
MAX(substring(c.JudgmentDate, 1,6))
FROM
BBNew.dbo.fffCases as c
inner join
BBNew.dbo.fffCourtCodes as cc
on c.CourtCode = cc.MST_COURT_CODE
inner join
BBNew.dbo.fffCourtTypes as ct
on c.CourtType = ct.CTcode
inner join
BBNew.dbo.fffRecordTypes as rt
on c.FilingType = rt.RTcode
WHERE
c.StateFips LIKE :Param_State
OR c.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
GROUP BY
c.StateFips,
cc.MST_COURT_NAME,
ct.CTdefinition,
rt.RTdefinition;
Be sure to have an index by:
- BBNew.dbo.fffCases.PostedDate <-- clustered one
- BBNew.dbo.fffCases.StateFips
- BBNew.dbo.fffCases.CourtCode
- BBNew.dbo.fffCases.CourtType
- BBNew.dbo.fffCases.FilingType
- BBNew.dbo.fffCourtCodes.MST_COURT_CODE
- BBNew.dbo.fffCourtTypes.CTcode
- BBNew.dbo.fffRecordTypes.rt.RTcode
AMB
"James Juno" wrote:
> Chandra,
> This is great. Thank you so much. I haven't tried it yet - but it looks good.
> James.
> "Chandra" wrote:
> > Hi James
> > I added parenthesis to your query:
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,
> > MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> > MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> >
> > FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> > BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> >
> > WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND
> > BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> >
> > (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> > OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> > OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> >
> > GROUP BY
> > BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> >
> > Please let me know if this is correct.
> >
> > If it is correct, then there is no need of changing the query, your query
> > looks perfect.
> > There might be Primary Keys declared on the tables that are used here.
> >
> > Else create Index on the columns that are involved in the where clause.
> >
> > please let me know your comments
> >
> > thanks and regards
> > Chandra
> >
> > "James Juno" wrote:
> >
> > > Dear Forum,
> > >
> > > Please help me modify this query for optimum performance and suggest indexes
> > > to create. Thanks. James
> > >
> > > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > BBNew.dbo.fffRecordTypes.RTdefinition,
> > > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > BBNew.dbo.fffRecordTypes.RTdefinition
> > >|||Alejandro,
Great. Thank you.
James
"Alejandro Mesa" wrote:
> To avoid confusion, use ansi join instead old style. Also use alias to make
> the statement more readable.
> SELECT
> cc.MST_COURT_NAME,
> ct.CTdefinition,
> rt.RTdefinition,
> c.StateFips,
> MIN(substring(c.JudgmentDate, 1,6)),
> MAX(substring(c.JudgmentDate, 1,6))
> FROM
> BBNew.dbo.fffCases as c
> inner join
> BBNew.dbo.fffCourtCodes as cc
> on c.CourtCode = cc.MST_COURT_CODE
> inner join
> BBNew.dbo.fffCourtTypes as ct
> on c.CourtType = ct.CTcode
> inner join
> BBNew.dbo.fffRecordTypes as rt
> on c.FilingType = rt.RTcode
> WHERE
> c.StateFips LIKE :Param_State
> OR c.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> GROUP BY
> c.StateFips,
> cc.MST_COURT_NAME,
> ct.CTdefinition,
> rt.RTdefinition;
> Be sure to have an index by:
> - BBNew.dbo.fffCases.PostedDate <-- clustered one
> - BBNew.dbo.fffCases.StateFips
> - BBNew.dbo.fffCases.CourtCode
> - BBNew.dbo.fffCases.CourtType
> - BBNew.dbo.fffCases.FilingType
> - BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> - BBNew.dbo.fffCourtTypes.CTcode
> - BBNew.dbo.fffRecordTypes.rt.RTcode
>
> AMB
>
> "James Juno" wrote:
> > Chandra,
> >
> > This is great. Thank you so much. I haven't tried it yet - but it looks good.
> >
> > James.
> >
> > "Chandra" wrote:
> >
> > > Hi James
> > > I added parenthesis to your query:
> > > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > BBNew.dbo.fffRecordTypes.RTdefinition,
> > > BBNew.dbo.fffCases.StateFips,
> > > MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,1,6)),
> > > MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6))
> > >
> > > FROM
> > > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes,
> > > BBNew.dbo.fffRecordTypes, BBNew.dbo.fffCases
> > >
> > > WHERE BBNew.dbo.fffCases.CourtCode = BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > > AND
> > > BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > >
> > > (BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode
> > > OR BBNew.dbo.fffCases.StateFips LIKE :Param_State
> > > OR BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate)
> > >
> > > GROUP BY
> > > BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > BBNew.dbo.fffCourtTypes.CTdefinition, BBNew.dbo.fffRecordTypes.RTdefinition
> > >
> > > Please let me know if this is correct.
> > >
> > > If it is correct, then there is no need of changing the query, your query
> > > looks perfect.
> > > There might be Primary Keys declared on the tables that are used here.
> > >
> > > Else create Index on the columns that are involved in the where clause.
> > >
> > > please let me know your comments
> > >
> > > thanks and regards
> > > Chandra
> > >
> > > "James Juno" wrote:
> > >
> > > > Dear Forum,
> > > >
> > > > Please help me modify this query for optimum performance and suggest indexes
> > > > to create. Thanks. James
> > > >
> > > > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > > BBNew.dbo.fffRecordTypes.RTdefinition,
> > > > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > > > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > > > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > > > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > > > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > > > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > > > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > > > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > > > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > > > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > > > BBNew.dbo.fffCourtTypes.CTdefinition,
> > > > BBNew.dbo.fffRecordTypes.RTdefinition
> > > >|||In general, it is a good practice to create a Primary Key constraint on
all tables (which will automatically create a unique index), and to
create indexes on foreign key constraints.
You did not post any DDL, so the keys and indexes cannot be reviewed.
But the query would benefit if all join columns were indexed.
Gert-Jan
James Juno wrote:
> Dear Forum,
> Please help me modify this query for optimum performance and suggest indexes
> to create. Thanks. James
> SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition,
> BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> BBNew.dbo.fffCourtTypes.CTdefinition,
> BBNew.dbo.fffRecordTypes.RTdefinition|||Gert-Jan,
I got the script that works. Thanks for your contribution.
James
"Gert-Jan Strik" wrote:
> In general, it is a good practice to create a Primary Key constraint on
> all tables (which will automatically create a unique index), and to
> create indexes on foreign key constraints.
> You did not post any DDL, so the keys and indexes cannot be reviewed.
> But the query would benefit if all join columns were indexed.
> Gert-Jan
>
> James Juno wrote:
> >
> > Dear Forum,
> >
> > Please help me modify this query for optimum performance and suggest indexes
> > to create. Thanks. James
> >
> > SELECT BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition,
> > BBNew.dbo.fffCases.StateFips,MIN(substring(BBNew.dbo.fffCASES.JudgmentDate,
> > 1,6)), MAX(substring(BBNew.dbo.fffCASES.JudgmentDate, 1,6)) FROM
> > BBNew.dbo.fffCourtCodes, BBNew.dbo.fffCourtTypes, BBNew.dbo.fffRecordTypes,
> > BBNew.dbo.fffCases WHERE BBNew.dbo.fffCases.CourtCode => > BBNew.dbo.fffCourtCodes.MST_COURT_CODE
> > AND BBNew.dbo.fffCases.CourtType = BBNew.dbo.fffCourtTypes.CTcode AND
> > BBNew.dbo.fffCases.FilingType = BBNew.dbo.fffRecordTypes.RTcode OR
> > BBNew.dbo.fffCases.StateFips LIKE :Param_State OR
> > BBNew.dbo.fffCases.PostedDate BETWEEN :Param_StartDate AND :Param_EndDate
> > GROUP BY BBNew.dbo.fffCASES.StateFips, BBNew.dbo.fffCourtCodes.MST_COURT_NAME,
> > BBNew.dbo.fffCourtTypes.CTdefinition,
> > BBNew.dbo.fffRecordTypes.RTdefinition
>
Tuesday, March 20, 2012
Query Performance
HI to All!
Is there anyway to improve the performance of Queries involving
Substring clause. i have indexes on the query columns but performance is
not good. e.g if i use a query without substring it gives me result in
less than 1 seconds over a more than 1.3 million rows and when i use
substring it takes more than 10 seconds.
Regards
Farid
*** Sent via Developersdex http://www.examnotes.net ***Sometimes there is and sometimes there is not. Either way - it starts with
you posting DDL and sample data.
Maybe you don't even need the substring function. How can we tell?
ML|||
Dear ML
i have to use Substring function because i have data like
600600-02-12345 here i 600600 means my center. i have to substring this
to identify records related to 600600 center.
i m issuing following query
select count(distinct(substring(output_ref,1,6)
)) as ns123
from tbl_Main_Con where day(load_date)= '7' and month(load_date)= '9'
and year(load_date)= '2005'
this query returns result in more than 10 seconds. how can i improve its
performance.
Regards,
*** Sent via Developersdex http://www.examnotes.net ***|||Change your WHERE clause to:
where
load_date >= '20050907'
and load_date < '20050908'
You may want to create an index on load_date or (load_date, output_ref).
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:OVWKl75sFHA.1256@.TK2MSFTNGP09.phx.gbl...
Dear ML
i have to use Substring function because i have data like
600600-02-12345 here i 600600 means my center. i have to substring this
to identify records related to 600600 center.
i m issuing following query
select count(distinct(substring(output_ref,1,6)
)) as ns123
from tbl_Main_Con where day(load_date)= '7' and month(load_date)= '9'
and year(load_date)= '2005'
this query returns result in more than 10 seconds. how can i improve its
performance.
Regards,
*** Sent via Developersdex http://www.examnotes.net ***|||Try using the LEFT function, although I doubt it would make any difference.
Are frequently sought columns indexed?
If you can, consider redesigning the table to store this piece of
information separately. Maybe even in a computed column, e.g. using the
following expression: left(output_ref, 6).
Read Tom's reply for another suggestion.
ML|||Just check with this
Initialize the parameter to NULL
SET @.Auto = 0
Detail.Automated = ISNULL(@.Auto ,Detail.Automated)
NOTE : Constraint Detail.Automated is not null
Detail.Automated = ISNULL(@.Auto ,Detail.Automated) and Detail.Automated IS
NULL
NOTE : Constraint Detail.Automated can have NULL values.
and Also check whether any index on the search arguments in the where caluse
.
HTH
Rajesh Peddireddy
"Jim Abel" wrote:
> The folling query works but is it the most efficient way to write it from
a
> performance aspect?
> The parameter can be set with the values (0,1 and %) these can be changed
if
> needed for the final query version.
> The Detail.Automated database field is a datatype of bit
> Should I be writing this to avoid the LIKE keyword?
>
> -- only used for testing
> DECLARE @.Auto AS char (1)
> SET @.Auto = '%'
> -- end test
> SELECT DISTINCT Status.ID,
> Info.ID,
> Info.Text,
> Detail.Automated,
> Status.Status
> FROM Status INNER JOIN Info
> ON Status.ID = Info.ID
> INNER JOIN Detail
> ON Info.ID = Detail.ID
> WHERE (Status.ID = 4)
> AND (Detail.Automated LIKE @.Auto|||check also indexes on the join fields
HTH
Rajesh Peddireddy
"Rajesh" wrote:
> Just check with this
> Initialize the parameter to NULL
> SET @.Auto = 0
> Detail.Automated = ISNULL(@.Auto ,Detail.Automated)
> NOTE : Constraint Detail.Automated is not null
> Detail.Automated = ISNULL(@.Auto ,Detail.Automated) and Detail.Automated I
S
> NULL
> NOTE : Constraint Detail.Automated can have NULL values.
> and Also check whether any index on the search arguments in the where calu
se.
> HTH
> Rajesh Peddireddy
> "Jim Abel" wrote:
>
Is there anyway to improve the performance of Queries involving
Substring clause. i have indexes on the query columns but performance is
not good. e.g if i use a query without substring it gives me result in
less than 1 seconds over a more than 1.3 million rows and when i use
substring it takes more than 10 seconds.
Regards
Farid
*** Sent via Developersdex http://www.examnotes.net ***Sometimes there is and sometimes there is not. Either way - it starts with
you posting DDL and sample data.
Maybe you don't even need the substring function. How can we tell?
ML|||
Dear ML
i have to use Substring function because i have data like
600600-02-12345 here i 600600 means my center. i have to substring this
to identify records related to 600600 center.
i m issuing following query
select count(distinct(substring(output_ref,1,6)
)) as ns123
from tbl_Main_Con where day(load_date)= '7' and month(load_date)= '9'
and year(load_date)= '2005'
this query returns result in more than 10 seconds. how can i improve its
performance.
Regards,
*** Sent via Developersdex http://www.examnotes.net ***|||Change your WHERE clause to:
where
load_date >= '20050907'
and load_date < '20050908'
You may want to create an index on load_date or (load_date, output_ref).
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"Ghulam Farid" <gfaryd@.yahoo.com> wrote in message
news:OVWKl75sFHA.1256@.TK2MSFTNGP09.phx.gbl...
Dear ML
i have to use Substring function because i have data like
600600-02-12345 here i 600600 means my center. i have to substring this
to identify records related to 600600 center.
i m issuing following query
select count(distinct(substring(output_ref,1,6)
)) as ns123
from tbl_Main_Con where day(load_date)= '7' and month(load_date)= '9'
and year(load_date)= '2005'
this query returns result in more than 10 seconds. how can i improve its
performance.
Regards,
*** Sent via Developersdex http://www.examnotes.net ***|||Try using the LEFT function, although I doubt it would make any difference.
Are frequently sought columns indexed?
If you can, consider redesigning the table to store this piece of
information separately. Maybe even in a computed column, e.g. using the
following expression: left(output_ref, 6).
Read Tom's reply for another suggestion.
ML|||Just check with this
Initialize the parameter to NULL
SET @.Auto = 0
Detail.Automated = ISNULL(@.Auto ,Detail.Automated)
NOTE : Constraint Detail.Automated is not null
Detail.Automated = ISNULL(@.Auto ,Detail.Automated) and Detail.Automated IS
NULL
NOTE : Constraint Detail.Automated can have NULL values.
and Also check whether any index on the search arguments in the where caluse
.
HTH
Rajesh Peddireddy
"Jim Abel" wrote:
> The folling query works but is it the most efficient way to write it from
a
> performance aspect?
> The parameter can be set with the values (0,1 and %) these can be changed
if
> needed for the final query version.
> The Detail.Automated database field is a datatype of bit
> Should I be writing this to avoid the LIKE keyword?
>
> -- only used for testing
> DECLARE @.Auto AS char (1)
> SET @.Auto = '%'
> -- end test
> SELECT DISTINCT Status.ID,
> Info.ID,
> Info.Text,
> Detail.Automated,
> Status.Status
> FROM Status INNER JOIN Info
> ON Status.ID = Info.ID
> INNER JOIN Detail
> ON Info.ID = Detail.ID
> WHERE (Status.ID = 4)
> AND (Detail.Automated LIKE @.Auto|||check also indexes on the join fields
HTH
Rajesh Peddireddy
"Rajesh" wrote:
> Just check with this
> Initialize the parameter to NULL
> SET @.Auto = 0
> Detail.Automated = ISNULL(@.Auto ,Detail.Automated)
> NOTE : Constraint Detail.Automated is not null
> Detail.Automated = ISNULL(@.Auto ,Detail.Automated) and Detail.Automated I
S
> NULL
> NOTE : Constraint Detail.Automated can have NULL values.
> and Also check whether any index on the search arguments in the where calu
se.
> HTH
> Rajesh Peddireddy
> "Jim Abel" wrote:
>
Query Performance
I have a vendor application running on SQL 2000 sp4. I've got a query
that isn't using any indexes:
select hsi.itemdata.itemnum, hsi.itemdata.itemname,
hsi.itemdata.batchnum, hsi.itemdata.status,
hsi.itemdata.itemtypegroupnum,
hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
hsi.itemdata.datestored, hsi.itemdata.usernum,
hsi.itemdata.deleteusernum,
hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
hsi.itemdata.institution, hsi.itemdata.maxdocrev
from hsi.itemdata
where hsi.itemdata.status + 0 = 0 and
( hsi.itemdata.itemtypenum = 101 or
hsi.itemdata.itemtypenum = 102 or
hsi.itemdata.itemtypenum = 103 or
hsi.itemdata.itemtypenum = 329 )
order by 8 desc
The table has 3,105,135 records in test. (There are 22,905,590 in
production.)
CREATE TABLE [hsi].[itemdata] (
[itemnum] [int] NOT NULL ,
[itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[batchnum] [int] NULL ,
[status] [int] NULL ,
[itemtypegroupnum] [int] NULL ,
[itemtypenum] [int] NULL ,
[itrevnum] [int] NULL ,
[itemdate] [datetime] NULL ,
[datestored] [datetime] NULL ,
[usernum] [int] NULL ,
[deleteusernum] [int] NULL ,
[securityvalue] [int] NULL ,
[doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[institution] [int] NULL ,
[maxdocrev] [int] NULL
) ON [DBSpace2]
GO
CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
[itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
GO
CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
[status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
GO
CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
[itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
[DBSpace2i]
GO
CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
[itemnum]) ON [DBSpace2]
GO
I would have thought it would use one of the existing indexes but it
just does a table scan.
I also tried:
CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
[itemtypenum]) ON [PRIMARY]
GO
But that didn't work either.
Any suggestions?
Thank you,
Toni
*** Sent via Developersdex http://www.developersdex.com ***> where hsi.itemdata.status + 0 = 0
Never do calculation on the column side if you can avoid. Why the + 0? wouldn't it be the same as:
where hsi.itemdata.status = 0
Now, whether an index on that columns can be used depends on the selectivity for the condition.
Also, if you have an index on the itemtypenum column, it might be used (4 times because of the ORs),
but again, whether or not it is used depends on the selectivity.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Toni" <teibner@.allinasql.com> wrote in message news:ul%23akXZYHHA.992@.TK2MSFTNGP02.phx.gbl...
>I have a vendor application running on SQL 2000 sp4. I've got a query
> that isn't using any indexes:
> select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> hsi.itemdata.batchnum, hsi.itemdata.status,
> hsi.itemdata.itemtypegroupnum,
> hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> hsi.itemdata.datestored, hsi.itemdata.usernum,
> hsi.itemdata.deleteusernum,
> hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> hsi.itemdata.institution, hsi.itemdata.maxdocrev
> from hsi.itemdata
> where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
> order by 8 desc
> The table has 3,105,135 records in test. (There are 22,905,590 in
> production.)
> CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
> ) ON [DBSpace2]
> GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> [DBSpace2i]
> GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> [itemnum]) ON [DBSpace2]
> GO
> I would have thought it would use one of the existing indexes but it
> just does a table scan.
> I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> [itemtypenum]) ON [PRIMARY]
> GO
> But that didn't work either.
> Any suggestions?
> Thank you,
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***|||The information provided raises more questions than it allows answers.
Indexes will only be used if the optimizer calculates that it will be
more efficient than a table scan. This generally depends on how
selective the indexes are, particularly for the values used in the
query.
How selective is itemtypenum? How selective are the specific values
of itemtypenum? What percentage of the table will the numbers
returned by the query represent?
SELECT hsi.itemdata.itemtypenum, count(*) as Rows
FROM hsi.itemdata
WHERE hsi.itemdata.itemtypenum IN (101, 102, 103, 329)
GROUP BY hsi.itemdata.itemtypenum
How selective is the test " hsi.itemdata.status + 0 = 0"? What data
type is hsi.itemdata.status? Why the + 0? It is possible that IF a
relatively small percentage of rows have a status = 0 that including
status in an index might help - but not while the + 0 is there.
Roy Harvey
Beacon Falls, CT
On Thu, 08 Mar 2007 07:24:39 -0800, Toni <teibner@.allinasql.com>
wrote:
>I have a vendor application running on SQL 2000 sp4. I've got a query
>that isn't using any indexes:
>select hsi.itemdata.itemnum, hsi.itemdata.itemname,
>hsi.itemdata.batchnum, hsi.itemdata.status,
>hsi.itemdata.itemtypegroupnum,
>hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
>hsi.itemdata.datestored, hsi.itemdata.usernum,
>hsi.itemdata.deleteusernum,
>hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
>hsi.itemdata.institution, hsi.itemdata.maxdocrev
>from hsi.itemdata
>where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
>order by 8 desc
>The table has 3,105,135 records in test. (There are 22,905,590 in
>production.)
>CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
>,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
>) ON [DBSpace2]
>GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
>[itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
>GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
>[status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
>GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
>[itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
>[DBSpace2i]
>GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
>[itemnum]) ON [DBSpace2]
>GO
>I would have thought it would use one of the existing indexes but it
>just does a table scan.
>I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
>[itemtypenum]) ON [PRIMARY]
>GO
>But that didn't work either.
>Any suggestions?
>Thank you,
>Toni
>*** Sent via Developersdex http://www.developersdex.com ***|||what is the ORDER By 8?
--
Jack Vamvas
___________________________________
The latest IT jobs - www.ITjobfeed.com
<a href="http://links.10026.com/?link=uk/">http://www.itjobfeed.com">UK IT Jobs</a>
"Toni" <teibner@.allinasql.com> wrote in message
news:ul%23akXZYHHA.992@.TK2MSFTNGP02.phx.gbl...
>I have a vendor application running on SQL 2000 sp4. I've got a query
> that isn't using any indexes:
> select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> hsi.itemdata.batchnum, hsi.itemdata.status,
> hsi.itemdata.itemtypegroupnum,
> hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> hsi.itemdata.datestored, hsi.itemdata.usernum,
> hsi.itemdata.deleteusernum,
> hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> hsi.itemdata.institution, hsi.itemdata.maxdocrev
> from hsi.itemdata
> where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
> order by 8 desc
> The table has 3,105,135 records in test. (There are 22,905,590 in
> production.)
> CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
> ) ON [DBSpace2]
> GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> [DBSpace2i]
> GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> [itemnum]) ON [DBSpace2]
> GO
> I would have thought it would use one of the existing indexes but it
> just does a table scan.
> I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> [itemtypenum]) ON [PRIMARY]
> GO
> But that didn't work either.
> Any suggestions?
> Thank you,
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***|||On Thu, 8 Mar 2007 17:03:46 -0000, "Jack Vamvas"
<DEL_TO_REPLY@.del.com> wrote:
>what is the ORDER By 8?
That says to order the eighth column in the SELECT list. My
understanding is that this syntax is deprecated in the SQL standard,
and it certainly has no place in production code, but it is a handy
shortcut when doing quick and dirty SQL.
Roy Harvey
Beacon Falls, CT|||On Thu, 08 Mar 2007 12:22:58 -0500, Roy Harvey <roy_harvey@.snet.net>
wrote:
>That says to order the eighth column in the SELECT list.
Make that:
That says to order BY the eighth column in the SELECT list.|||Toni,
it is not a good practice to manipulate columns in the "where" clause. SQL
Server will not try to use statistics for those columnas in case they exists.
> hsi.itemdata.status + 0 = 0
Compare the execution plans between that statement and one using:
...
where
hsi.itemdata.status = 0 and
( hsi.itemdata.itemtypenum = 101 or
hsi.itemdata.itemtypenum = 102 or
hsi.itemdata.itemtypenum = 103 or
hsi.itemdata.itemtypenum = 329 )
...
Also check fragmentation (dbcc showcontig) in that table and, if possible,
create a clustered index. It has some rows for a heap.
AMB
"Toni" wrote:
> I have a vendor application running on SQL 2000 sp4. I've got a query
> that isn't using any indexes:
> select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> hsi.itemdata.batchnum, hsi.itemdata.status,
> hsi.itemdata.itemtypegroupnum,
> hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> hsi.itemdata.datestored, hsi.itemdata.usernum,
> hsi.itemdata.deleteusernum,
> hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> hsi.itemdata.institution, hsi.itemdata.maxdocrev
> from hsi.itemdata
> where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
> order by 8 desc
> The table has 3,105,135 records in test. (There are 22,905,590 in
> production.)
> CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
> ) ON [DBSpace2]
> GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> [DBSpace2i]
> GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> [itemnum]) ON [DBSpace2]
> GO
> I would have thought it would use one of the existing indexes but it
> just does a table scan.
> I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> [itemtypenum]) ON [PRIMARY]
> GO
> But that didn't work either.
> Any suggestions?
> Thank you,
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***
>|||Jack Vamvas,
> what is the ORDER By 8?
Order by the 8th column in the resultset. Not a good practice to use on
production code.
AMB
"Jack Vamvas" wrote:
> what is the ORDER By 8?
> --
> Jack Vamvas
> ___________________________________
> The latest IT jobs - www.ITjobfeed.com
> <a href="http://links.10026.com/?link=uk/">http://www.itjobfeed.com">UK IT Jobs</a>
>
> "Toni" <teibner@.allinasql.com> wrote in message
> news:ul%23akXZYHHA.992@.TK2MSFTNGP02.phx.gbl...
> >I have a vendor application running on SQL 2000 sp4. I've got a query
> > that isn't using any indexes:
> >
> > select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> > hsi.itemdata.batchnum, hsi.itemdata.status,
> > hsi.itemdata.itemtypegroupnum,
> > hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> > hsi.itemdata.datestored, hsi.itemdata.usernum,
> > hsi.itemdata.deleteusernum,
> > hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> > hsi.itemdata.institution, hsi.itemdata.maxdocrev
> > from hsi.itemdata
> > where hsi.itemdata.status + 0 = 0 and
> > ( hsi.itemdata.itemtypenum = 101 or
> > hsi.itemdata.itemtypenum = 102 or
> > hsi.itemdata.itemtypenum = 103 or
> > hsi.itemdata.itemtypenum = 329 )
> > order by 8 desc
> >
> > The table has 3,105,135 records in test. (There are 22,905,590 in
> > production.)
> >
> > CREATE TABLE [hsi].[itemdata] (
> > [itemnum] [int] NOT NULL ,
> > [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> > [batchnum] [int] NULL ,
> > [status] [int] NULL ,
> > [itemtypegroupnum] [int] NULL ,
> > [itemtypenum] [int] NULL ,
> > [itrevnum] [int] NULL ,
> > [itemdate] [datetime] NULL ,
> > [datestored] [datetime] NULL ,
> > [usernum] [int] NULL ,
> > [deleteusernum] [int] NULL ,
> > [securityvalue] [int] NULL ,
> > [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> > ,
> > [institution] [int] NULL ,
> > [maxdocrev] [int] NULL
> > ) ON [DBSpace2]
> > GO
> >
> > CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> > [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> > GO
> >
> > CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> > [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> > GO
> >
> > CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> > [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> > [DBSpace2i]
> > GO
> >
> > CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> > [itemnum]) ON [DBSpace2]
> > GO
> >
> > I would have thought it would use one of the existing indexes but it
> > just does a table scan.
> >
> > I also tried:
> > CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> > [itemtypenum]) ON [PRIMARY]
> > GO
> > But that didn't work either.
> > Any suggestions?
> >
> > Thank you,
> > Toni
> >
> > *** Sent via Developersdex http://www.developersdex.com ***
>
>
that isn't using any indexes:
select hsi.itemdata.itemnum, hsi.itemdata.itemname,
hsi.itemdata.batchnum, hsi.itemdata.status,
hsi.itemdata.itemtypegroupnum,
hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
hsi.itemdata.datestored, hsi.itemdata.usernum,
hsi.itemdata.deleteusernum,
hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
hsi.itemdata.institution, hsi.itemdata.maxdocrev
from hsi.itemdata
where hsi.itemdata.status + 0 = 0 and
( hsi.itemdata.itemtypenum = 101 or
hsi.itemdata.itemtypenum = 102 or
hsi.itemdata.itemtypenum = 103 or
hsi.itemdata.itemtypenum = 329 )
order by 8 desc
The table has 3,105,135 records in test. (There are 22,905,590 in
production.)
CREATE TABLE [hsi].[itemdata] (
[itemnum] [int] NOT NULL ,
[itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[batchnum] [int] NULL ,
[status] [int] NULL ,
[itemtypegroupnum] [int] NULL ,
[itemtypenum] [int] NULL ,
[itrevnum] [int] NULL ,
[itemdate] [datetime] NULL ,
[datestored] [datetime] NULL ,
[usernum] [int] NULL ,
[deleteusernum] [int] NULL ,
[securityvalue] [int] NULL ,
[doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[institution] [int] NULL ,
[maxdocrev] [int] NULL
) ON [DBSpace2]
GO
CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
[itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
GO
CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
[status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
GO
CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
[itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
[DBSpace2i]
GO
CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
[itemnum]) ON [DBSpace2]
GO
I would have thought it would use one of the existing indexes but it
just does a table scan.
I also tried:
CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
[itemtypenum]) ON [PRIMARY]
GO
But that didn't work either.
Any suggestions?
Thank you,
Toni
*** Sent via Developersdex http://www.developersdex.com ***> where hsi.itemdata.status + 0 = 0
Never do calculation on the column side if you can avoid. Why the + 0? wouldn't it be the same as:
where hsi.itemdata.status = 0
Now, whether an index on that columns can be used depends on the selectivity for the condition.
Also, if you have an index on the itemtypenum column, it might be used (4 times because of the ORs),
but again, whether or not it is used depends on the selectivity.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
"Toni" <teibner@.allinasql.com> wrote in message news:ul%23akXZYHHA.992@.TK2MSFTNGP02.phx.gbl...
>I have a vendor application running on SQL 2000 sp4. I've got a query
> that isn't using any indexes:
> select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> hsi.itemdata.batchnum, hsi.itemdata.status,
> hsi.itemdata.itemtypegroupnum,
> hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> hsi.itemdata.datestored, hsi.itemdata.usernum,
> hsi.itemdata.deleteusernum,
> hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> hsi.itemdata.institution, hsi.itemdata.maxdocrev
> from hsi.itemdata
> where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
> order by 8 desc
> The table has 3,105,135 records in test. (There are 22,905,590 in
> production.)
> CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
> ) ON [DBSpace2]
> GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> [DBSpace2i]
> GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> [itemnum]) ON [DBSpace2]
> GO
> I would have thought it would use one of the existing indexes but it
> just does a table scan.
> I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> [itemtypenum]) ON [PRIMARY]
> GO
> But that didn't work either.
> Any suggestions?
> Thank you,
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***|||The information provided raises more questions than it allows answers.
Indexes will only be used if the optimizer calculates that it will be
more efficient than a table scan. This generally depends on how
selective the indexes are, particularly for the values used in the
query.
How selective is itemtypenum? How selective are the specific values
of itemtypenum? What percentage of the table will the numbers
returned by the query represent?
SELECT hsi.itemdata.itemtypenum, count(*) as Rows
FROM hsi.itemdata
WHERE hsi.itemdata.itemtypenum IN (101, 102, 103, 329)
GROUP BY hsi.itemdata.itemtypenum
How selective is the test " hsi.itemdata.status + 0 = 0"? What data
type is hsi.itemdata.status? Why the + 0? It is possible that IF a
relatively small percentage of rows have a status = 0 that including
status in an index might help - but not while the + 0 is there.
Roy Harvey
Beacon Falls, CT
On Thu, 08 Mar 2007 07:24:39 -0800, Toni <teibner@.allinasql.com>
wrote:
>I have a vendor application running on SQL 2000 sp4. I've got a query
>that isn't using any indexes:
>select hsi.itemdata.itemnum, hsi.itemdata.itemname,
>hsi.itemdata.batchnum, hsi.itemdata.status,
>hsi.itemdata.itemtypegroupnum,
>hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
>hsi.itemdata.datestored, hsi.itemdata.usernum,
>hsi.itemdata.deleteusernum,
>hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
>hsi.itemdata.institution, hsi.itemdata.maxdocrev
>from hsi.itemdata
>where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
>order by 8 desc
>The table has 3,105,135 records in test. (There are 22,905,590 in
>production.)
>CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
>,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
>) ON [DBSpace2]
>GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
>[itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
>GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
>[status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
>GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
>[itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
>[DBSpace2i]
>GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
>[itemnum]) ON [DBSpace2]
>GO
>I would have thought it would use one of the existing indexes but it
>just does a table scan.
>I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
>[itemtypenum]) ON [PRIMARY]
>GO
>But that didn't work either.
>Any suggestions?
>Thank you,
>Toni
>*** Sent via Developersdex http://www.developersdex.com ***|||what is the ORDER By 8?
--
Jack Vamvas
___________________________________
The latest IT jobs - www.ITjobfeed.com
<a href="http://links.10026.com/?link=uk/">http://www.itjobfeed.com">UK IT Jobs</a>
"Toni" <teibner@.allinasql.com> wrote in message
news:ul%23akXZYHHA.992@.TK2MSFTNGP02.phx.gbl...
>I have a vendor application running on SQL 2000 sp4. I've got a query
> that isn't using any indexes:
> select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> hsi.itemdata.batchnum, hsi.itemdata.status,
> hsi.itemdata.itemtypegroupnum,
> hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> hsi.itemdata.datestored, hsi.itemdata.usernum,
> hsi.itemdata.deleteusernum,
> hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> hsi.itemdata.institution, hsi.itemdata.maxdocrev
> from hsi.itemdata
> where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
> order by 8 desc
> The table has 3,105,135 records in test. (There are 22,905,590 in
> production.)
> CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
> ) ON [DBSpace2]
> GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> [DBSpace2i]
> GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> [itemnum]) ON [DBSpace2]
> GO
> I would have thought it would use one of the existing indexes but it
> just does a table scan.
> I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> [itemtypenum]) ON [PRIMARY]
> GO
> But that didn't work either.
> Any suggestions?
> Thank you,
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***|||On Thu, 8 Mar 2007 17:03:46 -0000, "Jack Vamvas"
<DEL_TO_REPLY@.del.com> wrote:
>what is the ORDER By 8?
That says to order the eighth column in the SELECT list. My
understanding is that this syntax is deprecated in the SQL standard,
and it certainly has no place in production code, but it is a handy
shortcut when doing quick and dirty SQL.
Roy Harvey
Beacon Falls, CT|||On Thu, 08 Mar 2007 12:22:58 -0500, Roy Harvey <roy_harvey@.snet.net>
wrote:
>That says to order the eighth column in the SELECT list.
Make that:
That says to order BY the eighth column in the SELECT list.|||Toni,
it is not a good practice to manipulate columns in the "where" clause. SQL
Server will not try to use statistics for those columnas in case they exists.
> hsi.itemdata.status + 0 = 0
Compare the execution plans between that statement and one using:
...
where
hsi.itemdata.status = 0 and
( hsi.itemdata.itemtypenum = 101 or
hsi.itemdata.itemtypenum = 102 or
hsi.itemdata.itemtypenum = 103 or
hsi.itemdata.itemtypenum = 329 )
...
Also check fragmentation (dbcc showcontig) in that table and, if possible,
create a clustered index. It has some rows for a heap.
AMB
"Toni" wrote:
> I have a vendor application running on SQL 2000 sp4. I've got a query
> that isn't using any indexes:
> select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> hsi.itemdata.batchnum, hsi.itemdata.status,
> hsi.itemdata.itemtypegroupnum,
> hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> hsi.itemdata.datestored, hsi.itemdata.usernum,
> hsi.itemdata.deleteusernum,
> hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> hsi.itemdata.institution, hsi.itemdata.maxdocrev
> from hsi.itemdata
> where hsi.itemdata.status + 0 = 0 and
> ( hsi.itemdata.itemtypenum = 101 or
> hsi.itemdata.itemtypenum = 102 or
> hsi.itemdata.itemtypenum = 103 or
> hsi.itemdata.itemtypenum = 329 )
> order by 8 desc
> The table has 3,105,135 records in test. (There are 22,905,590 in
> production.)
> CREATE TABLE [hsi].[itemdata] (
> [itemnum] [int] NOT NULL ,
> [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [batchnum] [int] NULL ,
> [status] [int] NULL ,
> [itemtypegroupnum] [int] NULL ,
> [itemtypenum] [int] NULL ,
> [itrevnum] [int] NULL ,
> [itemdate] [datetime] NULL ,
> [datestored] [datetime] NULL ,
> [usernum] [int] NULL ,
> [deleteusernum] [int] NULL ,
> [securityvalue] [int] NULL ,
> [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ,
> [institution] [int] NULL ,
> [maxdocrev] [int] NULL
> ) ON [DBSpace2]
> GO
> CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> GO
> CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> [DBSpace2i]
> GO
> CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> [itemnum]) ON [DBSpace2]
> GO
> I would have thought it would use one of the existing indexes but it
> just does a table scan.
> I also tried:
> CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> [itemtypenum]) ON [PRIMARY]
> GO
> But that didn't work either.
> Any suggestions?
> Thank you,
> Toni
> *** Sent via Developersdex http://www.developersdex.com ***
>|||Jack Vamvas,
> what is the ORDER By 8?
Order by the 8th column in the resultset. Not a good practice to use on
production code.
AMB
"Jack Vamvas" wrote:
> what is the ORDER By 8?
> --
> Jack Vamvas
> ___________________________________
> The latest IT jobs - www.ITjobfeed.com
> <a href="http://links.10026.com/?link=uk/">http://www.itjobfeed.com">UK IT Jobs</a>
>
> "Toni" <teibner@.allinasql.com> wrote in message
> news:ul%23akXZYHHA.992@.TK2MSFTNGP02.phx.gbl...
> >I have a vendor application running on SQL 2000 sp4. I've got a query
> > that isn't using any indexes:
> >
> > select hsi.itemdata.itemnum, hsi.itemdata.itemname,
> > hsi.itemdata.batchnum, hsi.itemdata.status,
> > hsi.itemdata.itemtypegroupnum,
> > hsi.itemdata.itemtypenum, hsi.itemdata.itrevnum, hsi.itemdata.itemdate,
> > hsi.itemdata.datestored, hsi.itemdata.usernum,
> > hsi.itemdata.deleteusernum,
> > hsi.itemdata.securityvalue, hsi.itemdata.doctracenumber,
> > hsi.itemdata.institution, hsi.itemdata.maxdocrev
> > from hsi.itemdata
> > where hsi.itemdata.status + 0 = 0 and
> > ( hsi.itemdata.itemtypenum = 101 or
> > hsi.itemdata.itemtypenum = 102 or
> > hsi.itemdata.itemtypenum = 103 or
> > hsi.itemdata.itemtypenum = 329 )
> > order by 8 desc
> >
> > The table has 3,105,135 records in test. (There are 22,905,590 in
> > production.)
> >
> > CREATE TABLE [hsi].[itemdata] (
> > [itemnum] [int] NOT NULL ,
> > [itemname] [char] (101) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> > [batchnum] [int] NULL ,
> > [status] [int] NULL ,
> > [itemtypegroupnum] [int] NULL ,
> > [itemtypenum] [int] NULL ,
> > [itrevnum] [int] NULL ,
> > [itemdate] [datetime] NULL ,
> > [datestored] [datetime] NULL ,
> > [usernum] [int] NULL ,
> > [deleteusernum] [int] NULL ,
> > [securityvalue] [int] NULL ,
> > [doctracenumber] [char] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> > ,
> > [institution] [int] NULL ,
> > [maxdocrev] [int] NULL
> > ) ON [DBSpace2]
> > GO
> >
> > CREATE INDEX [itemdata10] ON [hsi].[itemdata]([itemtypenum],
> > [itemdate] DESC , [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> > GO
> >
> > CREATE INDEX [itemdata13] ON [hsi].[itemdata]([batchnum], [itemnum],
> > [status]) WITH FILLFACTOR = 90 ON [DBSpace2i]
> > GO
> >
> > CREATE UNIQUE INDEX [itemdata9] ON [hsi].[itemdata]([itemnum],
> > [itemdate] DESC , [itemtypenum], [status]) WITH FILLFACTOR = 90 ON
> > [DBSpace2i]
> > GO
> >
> > CREATE INDEX [ccitemdata8] ON [hsi].[itemdata]([datestored],
> > [itemnum]) ON [DBSpace2]
> > GO
> >
> > I would have thought it would use one of the existing indexes but it
> > just does a table scan.
> >
> > I also tried:
> > CREATE INDEX [IX_itemdata] ON [hsi].[itemdata]([status],
> > [itemtypenum]) ON [PRIMARY]
> > GO
> > But that didn't work either.
> > Any suggestions?
> >
> > Thank you,
> > Toni
> >
> > *** Sent via Developersdex http://www.developersdex.com ***
>
>
Friday, March 9, 2012
Query Optimizer
Hi,
I have been given a database without any index to tune its indexes and
SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
create indexes in that and compare the results with SPs in MyDB1.
For a particular query (a LEFT JOIN), the execution plan showed that a table
scan(on parent table) and hash join was used. Therefore I created a PK on
parent table.
This time Query Optimizer used clustered index scan and nested loop and
totally the execution plan was quiet improved.
I used a single connection in Query Analyzer to run two copies of the query
on two different databases (MyDB1 without index, MyDB2 with index).
The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
far everything was as I expected.
But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
which had index, was slower. However Query Analyzer had indicated less
cost(12%)!
I turned Statistics Time on to make sure. Yes it was slower and took more
time to produce results.
I used ITW, it suggested exactly the same index as I had created. I
unchecked the Clustered option for the PK to make it nonclustered. A
bookmark lookup was added in execution plan but the query became very fast!
I tried to use different join hints and LEFT HASH JOIN improved query
performance again.
Please help me with these two questions:
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
Any help would be greatly appreciated.
Leila1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
It's usually not a bad guide.
It doesn't take into account what is in memory though - i.e. reading from
disk gives the same cost as reading from memory so if you have two queries on
the same data and the first gets all the data into memory then the first will
take a lot longer but the cost won't take this into account.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
I would use hints sparingly. It's better to recode so that the plan the
system retrieves is reasonable - but sometimes you have no choice.
"Leila" wrote:
> Hi,
> I have been given a database without any index to tune its indexes and
> SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> create indexes in that and compare the results with SPs in MyDB1.
> For a particular query (a LEFT JOIN), the execution plan showed that a table
> scan(on parent table) and hash join was used. Therefore I created a PK on
> parent table.
> This time Query Optimizer used clustered index scan and nested loop and
> totally the execution plan was quiet improved.
> I used a single connection in Query Analyzer to run two copies of the query
> on two different databases (MyDB1 without index, MyDB2 with index).
> The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
> far everything was as I expected.
> But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
> which had index, was slower. However Query Analyzer had indicated less
> cost(12%)!
> I turned Statistics Time on to make sure. Yes it was slower and took more
> time to produce results.
> I used ITW, it suggested exactly the same index as I had created. I
> unchecked the Clustered option for the PK to make it nonclustered. A
> bookmark lookup was added in execution plan but the query became very fast!
> I tried to use different join hints and LEFT HASH JOIN improved query
> performance again.
> Please help me with these two questions:
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> Any help would be greatly appreciated.
> Leila
>
>|||Thanks Nigel,
I changed the order of executing two queries to make sure that the second
query is not always using buffer which first query has prepared. Besides, I
run DBCC FREEPROCCHACHE and DBCC DROPCLEANBUFFERS before each query. But the
result was the same always.
Do you know why clustered PK might have poor performance, but when I change
that to nonclustered it improves considerably?
Thanks,
Leila
"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:C6599BA9-1E50-41BE-A5F4-E15CAC597E84@.microsoft.com...
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> It's usually not a bad guide.
> It doesn't take into account what is in memory though - i.e. reading from
> disk gives the same cost as reading from memory so if you have two queries
on
> the same data and the first gets all the data into memory then the first
will
> take a lot longer but the cost won't take this into account.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> I would use hints sparingly. It's better to recode so that the plan the
> system retrieves is reasonable - but sometimes you have no choice.
> "Leila" wrote:
> > Hi,
> > I have been given a database without any index to tune its indexes and
> > SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> > create indexes in that and compare the results with SPs in MyDB1.
> > For a particular query (a LEFT JOIN), the execution plan showed that a
table
> > scan(on parent table) and hash join was used. Therefore I created a PK
on
> > parent table.
> > This time Query Optimizer used clustered index scan and nested loop and
> > totally the execution plan was quiet improved.
> > I used a single connection in Query Analyzer to run two copies of the
query
> > on two different databases (MyDB1 without index, MyDB2 with index).
> > The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2
12%. So
> > far everything was as I expected.
> > But when I run query on MyDB1 and MyDB2 several times, I noticed that
MyDB2
> > which had index, was slower. However Query Analyzer had indicated less
> > cost(12%)!
> > I turned Statistics Time on to make sure. Yes it was slower and took
more
> > time to produce results.
> > I used ITW, it suggested exactly the same index as I had created. I
> > unchecked the Clustered option for the PK to make it nonclustered. A
> > bookmark lookup was added in execution plan but the query became very
fast!
> >
> > I tried to use different join hints and LEFT HASH JOIN improved query
> > performance again.
> > Please help me with these two questions:
> > 1) Can I rely to Query Cost (relative to the batch) to discover that
which
> > indexes help my query? In my case, I say NO because practically I got
poor
> > performance.
> > 2) If I get better performance by using join hints, is it wise to use
them
> > in SPs without letting the query optimizer to choose its own plan?
> >
> > Any help would be greatly appreciated.
> > Leila
> >
> >
> >
> >|||On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>Do you know why clustered PK might have poor performance, but when I change
>that to nonclustered it improves considerably?
Hi Leila,
That can happen in some situations. To understand, you need to know what
exactly is stored on the leaf pages (the lowest level in the B-tree) of
each kind of index.
For the clustered index, the leaf pages contain all data from the table.
If the size of one row is 400 bytes, each clustered index leaf page will
contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
For a clustered index, the leaf pages contain the values of the indexed
columns plus a "row-locator" - usually the values of the clustered index'
columns; a physical row locator if there is no clustered index. If the
combined size of all columns in the index plus the row locator is 40
bytes, each nonclustered index leaf page will contain the information of
~200 rows ((8K - some overhead) / 40 bytes).
I think that your query used only columns that participate in the index
you used, but used a WHERE clause that prohibited the use of directly
finding the required rows in the index, so all rows had to be scanned. If
the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
index is clustered, but only 10 page reads if the index is nonclustered
(2000 / 200).
Remember: you'll lose this advantage as soon as you start referring to any
column that is not part of the index. The nonclustered index is cheaper
only because there is no need to locate and read the rest of the columns.
(This is called a "covering" index).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hugo,
Thank you very much indeed! Maybe you're right and the problem is from the
pages' density. On Monday I will go and check. As far as I know, the
developers have used some TEXT and NCHAR data types that might cause the low
density in pages.
Thanks again!
Leila
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:c9vcp09a0hmttg6c9t4nha093190q52314@.4ax.com...
> On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
> >Do you know why clustered PK might have poor performance, but when I
change
> >that to nonclustered it improves considerably?
> Hi Leila,
> That can happen in some situations. To understand, you need to know what
> exactly is stored on the leaf pages (the lowest level in the B-tree) of
> each kind of index.
> For the clustered index, the leaf pages contain all data from the table.
> If the size of one row is 400 bytes, each clustered index leaf page will
> contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
> For a clustered index, the leaf pages contain the values of the indexed
> columns plus a "row-locator" - usually the values of the clustered index'
> columns; a physical row locator if there is no clustered index. If the
> combined size of all columns in the index plus the row locator is 40
> bytes, each nonclustered index leaf page will contain the information of
> ~200 rows ((8K - some overhead) / 40 bytes).
> I think that your query used only columns that participate in the index
> you used, but used a WHERE clause that prohibited the use of directly
> finding the required rows in the index, so all rows had to be scanned. If
> the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
> index is clustered, but only 10 page reads if the index is nonclustered
> (2000 / 200).
> Remember: you'll lose this advantage as soon as you start referring to any
> column that is not part of the index. The nonclustered index is cheaper
> only because there is no need to locate and read the rest of the columns.
> (This is called a "covering" index).
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)|||On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
>But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
>which had index, was slower. However Query Analyzer had indicated less
>cost(12%)!
...
>I tried to use different join hints and LEFT HASH JOIN improved query
>performance again.
>Please help me with these two questions:
>1) Can I rely to Query Cost (relative to the batch) to discover that which
>indexes help my query? In my case, I say NO because practically I got poor
>performance.
>2) If I get better performance by using join hints, is it wise to use them
>in SPs without letting the query optimizer to choose its own plan?
>Any help would be greatly appreciated.
You tell an interesting story.
Let's review what the optimizer is all about - no, let's go even
further and consider what optimization is all about! If you have two
tables and no indexes, any join requires the work of a full
cross-join. Optimization suggests you have an index at least on the
PK (SQLServer does that automatically, though it calls the PK a
constraint, only if you don't even have a PK do you really have no
indexes). Especially when building an index also includes keeping
statistics (as it usually does in SQLServer), an optimizer can then
decide if a particular query can use the index. In a lot of cases,
this will drop a thirty-second query to thirty milliseconds. It's
those big, fat wins that the *optimizer* is all about.
Now, it so happens that, if your query requires that some large
percentage of records have to be inspected, that the use of any index
becomes a tougher decision. In general, if your query requires more
than about a quarter of a table's records, it turns out to be cheaper
to scan the whole table than to use a (nonclustered) index (if the
records that need to be scanned fit a range in a *clustered* index,
you get other situations). What I'm trying to say here is that if
it's a relatively close decision, the optimizer can guess wrong. If
it's only a factor of 2x, I'd guess the optimizer will get it wrong
fairly often. The optimizer is about finding 10x and 100x wins, and
it's pretty good at that.
So, I'm guessing your test query involved a large percentage of your
total records, and your database was relatively small, maybe under
100mb or 1m records in the tables involved.
Another thing to try is to put a few more indexes on each table, so
that the optimizer has more statistics to work with. Just a single
index on the left table may not be enough for the optimizer. Would
the ITW tell you if more would help? I'm not sure, I've only played
with that, never tried to use it for real.
I think I've only found one case where I had to use a hint to improve
performance, and that was instead of adding another index, which might
have been a better solution. If you tell SQLServer about the PKs for
each table, and put indices on the obvious fields of the larger
tables, the optimizer is going to give you 90%++ of available
performance. Um, that assumes the database is reasonably normalized.
Further hand-tuning of queries, doing semantic splits of tables, using
materialized views, and tuning physical layouts, and other exotica,
are still there for really huge or really critical databases.
I really enjoy tuning SQLServer, I'm just getting into some of the
available tools and techniques, but so far, in the real world, it
seems what I really find on people's sites are the real basics, tables
with no indexes on heavily used fields, etc. The good news is I put
on the index, show a 99% improvement in time, and everybody is happy.
The bad news is I don't get to use all the fancy stuff.
HTH,
J.|||Thank you very much for your comprehensive explanation.
Actually I'm not too bad at optimization and I guess the problem is
something beyond optimization concepts.
ITW's suggestion was what I had done. The query cost was quite reasonable
after creating index and the execution plan improved. But the execution time
got worse. The clustered PK which helped plan, caused a slower query while
nonclustered PK made it faster.
I have to study more on two tables' structure to find out the reason.
Thanks again,
Leila
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:4e3fp0tq3g00vd7k9a64r73pd6ld6rarbe@.4ax.com...
> On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
> >But when I run query on MyDB1 and MyDB2 several times, I noticed that
MyDB2
> >which had index, was slower. However Query Analyzer had indicated less
> >cost(12%)!
> ...
> >I tried to use different join hints and LEFT HASH JOIN improved query
> >performance again.
> >Please help me with these two questions:
> >1) Can I rely to Query Cost (relative to the batch) to discover that
which
> >indexes help my query? In my case, I say NO because practically I got
poor
> >performance.
> >2) If I get better performance by using join hints, is it wise to use
them
> >in SPs without letting the query optimizer to choose its own plan?
> >
> >Any help would be greatly appreciated.
> You tell an interesting story.
> Let's review what the optimizer is all about - no, let's go even
> further and consider what optimization is all about! If you have two
> tables and no indexes, any join requires the work of a full
> cross-join. Optimization suggests you have an index at least on the
> PK (SQLServer does that automatically, though it calls the PK a
> constraint, only if you don't even have a PK do you really have no
> indexes). Especially when building an index also includes keeping
> statistics (as it usually does in SQLServer), an optimizer can then
> decide if a particular query can use the index. In a lot of cases,
> this will drop a thirty-second query to thirty milliseconds. It's
> those big, fat wins that the *optimizer* is all about.
> Now, it so happens that, if your query requires that some large
> percentage of records have to be inspected, that the use of any index
> becomes a tougher decision. In general, if your query requires more
> than about a quarter of a table's records, it turns out to be cheaper
> to scan the whole table than to use a (nonclustered) index (if the
> records that need to be scanned fit a range in a *clustered* index,
> you get other situations). What I'm trying to say here is that if
> it's a relatively close decision, the optimizer can guess wrong. If
> it's only a factor of 2x, I'd guess the optimizer will get it wrong
> fairly often. The optimizer is about finding 10x and 100x wins, and
> it's pretty good at that.
> So, I'm guessing your test query involved a large percentage of your
> total records, and your database was relatively small, maybe under
> 100mb or 1m records in the tables involved.
> Another thing to try is to put a few more indexes on each table, so
> that the optimizer has more statistics to work with. Just a single
> index on the left table may not be enough for the optimizer. Would
> the ITW tell you if more would help? I'm not sure, I've only played
> with that, never tried to use it for real.
> I think I've only found one case where I had to use a hint to improve
> performance, and that was instead of adding another index, which might
> have been a better solution. If you tell SQLServer about the PKs for
> each table, and put indices on the obvious fields of the larger
> tables, the optimizer is going to give you 90%++ of available
> performance. Um, that assumes the database is reasonably normalized.
> Further hand-tuning of queries, doing semantic splits of tables, using
> materialized views, and tuning physical layouts, and other exotica,
> are still there for really huge or really critical databases.
> I really enjoy tuning SQLServer, I'm just getting into some of the
> available tools and techniques, but so far, in the real world, it
> seems what I really find on people's sites are the real basics, tables
> with no indexes on heavily used fields, etc. The good news is I put
> on the index, show a 99% improvement in time, and everybody is happy.
> The bad news is I don't get to use all the fancy stuff.
> HTH,
> J.
>
I have been given a database without any index to tune its indexes and
SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
create indexes in that and compare the results with SPs in MyDB1.
For a particular query (a LEFT JOIN), the execution plan showed that a table
scan(on parent table) and hash join was used. Therefore I created a PK on
parent table.
This time Query Optimizer used clustered index scan and nested loop and
totally the execution plan was quiet improved.
I used a single connection in Query Analyzer to run two copies of the query
on two different databases (MyDB1 without index, MyDB2 with index).
The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
far everything was as I expected.
But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
which had index, was slower. However Query Analyzer had indicated less
cost(12%)!
I turned Statistics Time on to make sure. Yes it was slower and took more
time to produce results.
I used ITW, it suggested exactly the same index as I had created. I
unchecked the Clustered option for the PK to make it nonclustered. A
bookmark lookup was added in execution plan but the query became very fast!
I tried to use different join hints and LEFT HASH JOIN improved query
performance again.
Please help me with these two questions:
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
Any help would be greatly appreciated.
Leila1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
It's usually not a bad guide.
It doesn't take into account what is in memory though - i.e. reading from
disk gives the same cost as reading from memory so if you have two queries on
the same data and the first gets all the data into memory then the first will
take a lot longer but the cost won't take this into account.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
I would use hints sparingly. It's better to recode so that the plan the
system retrieves is reasonable - but sometimes you have no choice.
"Leila" wrote:
> Hi,
> I have been given a database without any index to tune its indexes and
> SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> create indexes in that and compare the results with SPs in MyDB1.
> For a particular query (a LEFT JOIN), the execution plan showed that a table
> scan(on parent table) and hash join was used. Therefore I created a PK on
> parent table.
> This time Query Optimizer used clustered index scan and nested loop and
> totally the execution plan was quiet improved.
> I used a single connection in Query Analyzer to run two copies of the query
> on two different databases (MyDB1 without index, MyDB2 with index).
> The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
> far everything was as I expected.
> But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
> which had index, was slower. However Query Analyzer had indicated less
> cost(12%)!
> I turned Statistics Time on to make sure. Yes it was slower and took more
> time to produce results.
> I used ITW, it suggested exactly the same index as I had created. I
> unchecked the Clustered option for the PK to make it nonclustered. A
> bookmark lookup was added in execution plan but the query became very fast!
> I tried to use different join hints and LEFT HASH JOIN improved query
> performance again.
> Please help me with these two questions:
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> Any help would be greatly appreciated.
> Leila
>
>|||Thanks Nigel,
I changed the order of executing two queries to make sure that the second
query is not always using buffer which first query has prepared. Besides, I
run DBCC FREEPROCCHACHE and DBCC DROPCLEANBUFFERS before each query. But the
result was the same always.
Do you know why clustered PK might have poor performance, but when I change
that to nonclustered it improves considerably?
Thanks,
Leila
"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:C6599BA9-1E50-41BE-A5F4-E15CAC597E84@.microsoft.com...
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> It's usually not a bad guide.
> It doesn't take into account what is in memory though - i.e. reading from
> disk gives the same cost as reading from memory so if you have two queries
on
> the same data and the first gets all the data into memory then the first
will
> take a lot longer but the cost won't take this into account.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> I would use hints sparingly. It's better to recode so that the plan the
> system retrieves is reasonable - but sometimes you have no choice.
> "Leila" wrote:
> > Hi,
> > I have been given a database without any index to tune its indexes and
> > SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> > create indexes in that and compare the results with SPs in MyDB1.
> > For a particular query (a LEFT JOIN), the execution plan showed that a
table
> > scan(on parent table) and hash join was used. Therefore I created a PK
on
> > parent table.
> > This time Query Optimizer used clustered index scan and nested loop and
> > totally the execution plan was quiet improved.
> > I used a single connection in Query Analyzer to run two copies of the
query
> > on two different databases (MyDB1 without index, MyDB2 with index).
> > The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2
12%. So
> > far everything was as I expected.
> > But when I run query on MyDB1 and MyDB2 several times, I noticed that
MyDB2
> > which had index, was slower. However Query Analyzer had indicated less
> > cost(12%)!
> > I turned Statistics Time on to make sure. Yes it was slower and took
more
> > time to produce results.
> > I used ITW, it suggested exactly the same index as I had created. I
> > unchecked the Clustered option for the PK to make it nonclustered. A
> > bookmark lookup was added in execution plan but the query became very
fast!
> >
> > I tried to use different join hints and LEFT HASH JOIN improved query
> > performance again.
> > Please help me with these two questions:
> > 1) Can I rely to Query Cost (relative to the batch) to discover that
which
> > indexes help my query? In my case, I say NO because practically I got
poor
> > performance.
> > 2) If I get better performance by using join hints, is it wise to use
them
> > in SPs without letting the query optimizer to choose its own plan?
> >
> > Any help would be greatly appreciated.
> > Leila
> >
> >
> >
> >|||On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>Do you know why clustered PK might have poor performance, but when I change
>that to nonclustered it improves considerably?
Hi Leila,
That can happen in some situations. To understand, you need to know what
exactly is stored on the leaf pages (the lowest level in the B-tree) of
each kind of index.
For the clustered index, the leaf pages contain all data from the table.
If the size of one row is 400 bytes, each clustered index leaf page will
contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
For a clustered index, the leaf pages contain the values of the indexed
columns plus a "row-locator" - usually the values of the clustered index'
columns; a physical row locator if there is no clustered index. If the
combined size of all columns in the index plus the row locator is 40
bytes, each nonclustered index leaf page will contain the information of
~200 rows ((8K - some overhead) / 40 bytes).
I think that your query used only columns that participate in the index
you used, but used a WHERE clause that prohibited the use of directly
finding the required rows in the index, so all rows had to be scanned. If
the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
index is clustered, but only 10 page reads if the index is nonclustered
(2000 / 200).
Remember: you'll lose this advantage as soon as you start referring to any
column that is not part of the index. The nonclustered index is cheaper
only because there is no need to locate and read the rest of the columns.
(This is called a "covering" index).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hugo,
Thank you very much indeed! Maybe you're right and the problem is from the
pages' density. On Monday I will go and check. As far as I know, the
developers have used some TEXT and NCHAR data types that might cause the low
density in pages.
Thanks again!
Leila
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:c9vcp09a0hmttg6c9t4nha093190q52314@.4ax.com...
> On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
> >Do you know why clustered PK might have poor performance, but when I
change
> >that to nonclustered it improves considerably?
> Hi Leila,
> That can happen in some situations. To understand, you need to know what
> exactly is stored on the leaf pages (the lowest level in the B-tree) of
> each kind of index.
> For the clustered index, the leaf pages contain all data from the table.
> If the size of one row is 400 bytes, each clustered index leaf page will
> contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
> For a clustered index, the leaf pages contain the values of the indexed
> columns plus a "row-locator" - usually the values of the clustered index'
> columns; a physical row locator if there is no clustered index. If the
> combined size of all columns in the index plus the row locator is 40
> bytes, each nonclustered index leaf page will contain the information of
> ~200 rows ((8K - some overhead) / 40 bytes).
> I think that your query used only columns that participate in the index
> you used, but used a WHERE clause that prohibited the use of directly
> finding the required rows in the index, so all rows had to be scanned. If
> the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
> index is clustered, but only 10 page reads if the index is nonclustered
> (2000 / 200).
> Remember: you'll lose this advantage as soon as you start referring to any
> column that is not part of the index. The nonclustered index is cheaper
> only because there is no need to locate and read the rest of the columns.
> (This is called a "covering" index).
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)|||On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
>But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
>which had index, was slower. However Query Analyzer had indicated less
>cost(12%)!
...
>I tried to use different join hints and LEFT HASH JOIN improved query
>performance again.
>Please help me with these two questions:
>1) Can I rely to Query Cost (relative to the batch) to discover that which
>indexes help my query? In my case, I say NO because practically I got poor
>performance.
>2) If I get better performance by using join hints, is it wise to use them
>in SPs without letting the query optimizer to choose its own plan?
>Any help would be greatly appreciated.
You tell an interesting story.
Let's review what the optimizer is all about - no, let's go even
further and consider what optimization is all about! If you have two
tables and no indexes, any join requires the work of a full
cross-join. Optimization suggests you have an index at least on the
PK (SQLServer does that automatically, though it calls the PK a
constraint, only if you don't even have a PK do you really have no
indexes). Especially when building an index also includes keeping
statistics (as it usually does in SQLServer), an optimizer can then
decide if a particular query can use the index. In a lot of cases,
this will drop a thirty-second query to thirty milliseconds. It's
those big, fat wins that the *optimizer* is all about.
Now, it so happens that, if your query requires that some large
percentage of records have to be inspected, that the use of any index
becomes a tougher decision. In general, if your query requires more
than about a quarter of a table's records, it turns out to be cheaper
to scan the whole table than to use a (nonclustered) index (if the
records that need to be scanned fit a range in a *clustered* index,
you get other situations). What I'm trying to say here is that if
it's a relatively close decision, the optimizer can guess wrong. If
it's only a factor of 2x, I'd guess the optimizer will get it wrong
fairly often. The optimizer is about finding 10x and 100x wins, and
it's pretty good at that.
So, I'm guessing your test query involved a large percentage of your
total records, and your database was relatively small, maybe under
100mb or 1m records in the tables involved.
Another thing to try is to put a few more indexes on each table, so
that the optimizer has more statistics to work with. Just a single
index on the left table may not be enough for the optimizer. Would
the ITW tell you if more would help? I'm not sure, I've only played
with that, never tried to use it for real.
I think I've only found one case where I had to use a hint to improve
performance, and that was instead of adding another index, which might
have been a better solution. If you tell SQLServer about the PKs for
each table, and put indices on the obvious fields of the larger
tables, the optimizer is going to give you 90%++ of available
performance. Um, that assumes the database is reasonably normalized.
Further hand-tuning of queries, doing semantic splits of tables, using
materialized views, and tuning physical layouts, and other exotica,
are still there for really huge or really critical databases.
I really enjoy tuning SQLServer, I'm just getting into some of the
available tools and techniques, but so far, in the real world, it
seems what I really find on people's sites are the real basics, tables
with no indexes on heavily used fields, etc. The good news is I put
on the index, show a 99% improvement in time, and everybody is happy.
The bad news is I don't get to use all the fancy stuff.
HTH,
J.|||Thank you very much for your comprehensive explanation.
Actually I'm not too bad at optimization and I guess the problem is
something beyond optimization concepts.
ITW's suggestion was what I had done. The query cost was quite reasonable
after creating index and the execution plan improved. But the execution time
got worse. The clustered PK which helped plan, caused a slower query while
nonclustered PK made it faster.
I have to study more on two tables' structure to find out the reason.
Thanks again,
Leila
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:4e3fp0tq3g00vd7k9a64r73pd6ld6rarbe@.4ax.com...
> On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
> >But when I run query on MyDB1 and MyDB2 several times, I noticed that
MyDB2
> >which had index, was slower. However Query Analyzer had indicated less
> >cost(12%)!
> ...
> >I tried to use different join hints and LEFT HASH JOIN improved query
> >performance again.
> >Please help me with these two questions:
> >1) Can I rely to Query Cost (relative to the batch) to discover that
which
> >indexes help my query? In my case, I say NO because practically I got
poor
> >performance.
> >2) If I get better performance by using join hints, is it wise to use
them
> >in SPs without letting the query optimizer to choose its own plan?
> >
> >Any help would be greatly appreciated.
> You tell an interesting story.
> Let's review what the optimizer is all about - no, let's go even
> further and consider what optimization is all about! If you have two
> tables and no indexes, any join requires the work of a full
> cross-join. Optimization suggests you have an index at least on the
> PK (SQLServer does that automatically, though it calls the PK a
> constraint, only if you don't even have a PK do you really have no
> indexes). Especially when building an index also includes keeping
> statistics (as it usually does in SQLServer), an optimizer can then
> decide if a particular query can use the index. In a lot of cases,
> this will drop a thirty-second query to thirty milliseconds. It's
> those big, fat wins that the *optimizer* is all about.
> Now, it so happens that, if your query requires that some large
> percentage of records have to be inspected, that the use of any index
> becomes a tougher decision. In general, if your query requires more
> than about a quarter of a table's records, it turns out to be cheaper
> to scan the whole table than to use a (nonclustered) index (if the
> records that need to be scanned fit a range in a *clustered* index,
> you get other situations). What I'm trying to say here is that if
> it's a relatively close decision, the optimizer can guess wrong. If
> it's only a factor of 2x, I'd guess the optimizer will get it wrong
> fairly often. The optimizer is about finding 10x and 100x wins, and
> it's pretty good at that.
> So, I'm guessing your test query involved a large percentage of your
> total records, and your database was relatively small, maybe under
> 100mb or 1m records in the tables involved.
> Another thing to try is to put a few more indexes on each table, so
> that the optimizer has more statistics to work with. Just a single
> index on the left table may not be enough for the optimizer. Would
> the ITW tell you if more would help? I'm not sure, I've only played
> with that, never tried to use it for real.
> I think I've only found one case where I had to use a hint to improve
> performance, and that was instead of adding another index, which might
> have been a better solution. If you tell SQLServer about the PKs for
> each table, and put indices on the obvious fields of the larger
> tables, the optimizer is going to give you 90%++ of available
> performance. Um, that assumes the database is reasonably normalized.
> Further hand-tuning of queries, doing semantic splits of tables, using
> materialized views, and tuning physical layouts, and other exotica,
> are still there for really huge or really critical databases.
> I really enjoy tuning SQLServer, I'm just getting into some of the
> available tools and techniques, but so far, in the real world, it
> seems what I really find on people's sites are the real basics, tables
> with no indexes on heavily used fields, etc. The good news is I put
> on the index, show a 99% improvement in time, and everybody is happy.
> The bad news is I don't get to use all the fancy stuff.
> HTH,
> J.
>
Query Optimizer
Hi,
I have been given a database without any index to tune its indexes and
SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
create indexes in that and compare the results with SPs in MyDB1.
For a particular query (a LEFT JOIN), the execution plan showed that a table
scan(on parent table) and hash join was used. Therefore I created a PK on
parent table.
This time Query Optimizer used clustered index scan and nested loop and
totally the execution plan was quiet improved.
I used a single connection in Query Analyzer to run two copies of the query
on two different databases (MyDB1 without index, MyDB2 with index).
The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
far everything was as I expected.
But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
which had index, was slower. However Query Analyzer had indicated less
cost(12%)!
I turned Statistics Time on to make sure. Yes it was slower and took more
time to produce results.
I used ITW, it suggested exactly the same index as I had created. I
unchecked the Clustered option for the PK to make it nonclustered. A
bookmark lookup was added in execution plan but the query became very fast!
I tried to use different join hints and LEFT HASH JOIN improved query
performance again.
Please help me with these two questions:
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
Any help would be greatly appreciated.
Leila1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
It's usually not a bad guide.
It doesn't take into account what is in memory though - i.e. reading from
disk gives the same cost as reading from memory so if you have two queries o
n
the same data and the first gets all the data into memory then the first wil
l
take a lot longer but the cost won't take this into account.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
I would use hints sparingly. It's better to recode so that the plan the
system retrieves is reasonable - but sometimes you have no choice.
"Leila" wrote:
> Hi,
> I have been given a database without any index to tune its indexes and
> SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> create indexes in that and compare the results with SPs in MyDB1.
> For a particular query (a LEFT JOIN), the execution plan showed that a tab
le
> scan(on parent table) and hash join was used. Therefore I created a PK on
> parent table.
> This time Query Optimizer used clustered index scan and nested loop and
> totally the execution plan was quiet improved.
> I used a single connection in Query Analyzer to run two copies of the quer
y
> on two different databases (MyDB1 without index, MyDB2 with index).
> The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%.
So
> far everything was as I expected.
> But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB
2
> which had index, was slower. However Query Analyzer had indicated less
> cost(12%)!
> I turned Statistics Time on to make sure. Yes it was slower and took more
> time to produce results.
> I used ITW, it suggested exactly the same index as I had created. I
> unchecked the Clustered option for the PK to make it nonclustered. A
> bookmark lookup was added in execution plan but the query became very fast
!
> I tried to use different join hints and LEFT HASH JOIN improved query
> performance again.
> Please help me with these two questions:
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> Any help would be greatly appreciated.
> Leila
>
>|||Thanks Nigel,
I changed the order of executing two queries to make sure that the second
query is not always using buffer which first query has prepared. Besides, I
run DBCC FREEPROCCHACHE and DBCC DROPCLEANBUFFERS before each query. But the
result was the same always.
Do you know why clustered PK might have poor performance, but when I change
that to nonclustered it improves considerably?
Thanks,
Leila
"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:C6599BA9-1E50-41BE-A5F4-E15CAC597E84@.microsoft.com...
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> It's usually not a bad guide.
> It doesn't take into account what is in memory though - i.e. reading from
> disk gives the same cost as reading from memory so if you have two queries
on
> the same data and the first gets all the data into memory then the first
will[vbcol=seagreen]
> take a lot longer but the cost won't take this into account.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> I would use hints sparingly. It's better to recode so that the plan the
> system retrieves is reasonable - but sometimes you have no choice.
> "Leila" wrote:
>
table[vbcol=seagreen]
on[vbcol=seagreen]
query[vbcol=seagreen]
12%. So[vbcol=seagreen]
MyDB2[vbcol=seagreen]
more[vbcol=seagreen]
fast![vbcol=seagreen]
which[vbcol=seagreen]
poor[vbcol=seagreen]
them[vbcol=seagreen]|||On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>Do you know why clustered PK might have poor performance, but when I change
>that to nonclustered it improves considerably?
Hi Leila,
That can happen in some situations. To understand, you need to know what
exactly is stored on the leaf pages (the lowest level in the B-tree) of
each kind of index.
For the clustered index, the leaf pages contain all data from the table.
If the size of one row is 400 bytes, each clustered index leaf page will
contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
For a clustered index, the leaf pages contain the values of the indexed
columns plus a "row-locator" - usually the values of the clustered index'
columns; a physical row locator if there is no clustered index. If the
combined size of all columns in the index plus the row locator is 40
bytes, each nonclustered index leaf page will contain the information of
~200 rows ((8K - some overhead) / 40 bytes).
I think that your query used only columns that participate in the index
you used, but used a WHERE clause that prohibited the use of directly
finding the required rows in the index, so all rows had to be scanned. If
the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
index is clustered, but only 10 page reads if the index is nonclustered
(2000 / 200).
Remember: you'll lose this advantage as soon as you start referring to any
column that is not part of the index. The nonclustered index is cheaper
only because there is no need to locate and read the rest of the columns.
(This is called a "covering" index).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hugo,
Thank you very much indeed! Maybe you're right and the problem is from the
pages' density. On Monday I will go and check. As far as I know, the
developers have used some TEXT and NCHAR data types that might cause the low
density in pages.
Thanks again!
Leila
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:c9vcp09a0hmttg6c9t4nha093190q52314@.
4ax.com...
> On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>
change[vbcol=seagreen]
> Hi Leila,
> That can happen in some situations. To understand, you need to know what
> exactly is stored on the leaf pages (the lowest level in the B-tree) of
> each kind of index.
> For the clustered index, the leaf pages contain all data from the table.
> If the size of one row is 400 bytes, each clustered index leaf page will
> contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
> For a clustered index, the leaf pages contain the values of the indexed
> columns plus a "row-locator" - usually the values of the clustered index'
> columns; a physical row locator if there is no clustered index. If the
> combined size of all columns in the index plus the row locator is 40
> bytes, each nonclustered index leaf page will contain the information of
> ~200 rows ((8K - some overhead) / 40 bytes).
> I think that your query used only columns that participate in the index
> you used, but used a WHERE clause that prohibited the use of directly
> finding the required rows in the index, so all rows had to be scanned. If
> the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
> index is clustered, but only 10 page reads if the index is nonclustered
> (2000 / 200).
> Remember: you'll lose this advantage as soon as you start referring to any
> column that is not part of the index. The nonclustered index is cheaper
> only because there is no need to locate and read the rest of the columns.
> (This is called a "covering" index).
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)|||On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
>But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
>which had index, was slower. However Query Analyzer had indicated less
>cost(12%)!
...
>I tried to use different join hints and LEFT HASH JOIN improved query
>performance again.
>Please help me with these two questions:
>1) Can I rely to Query Cost (relative to the batch) to discover that which
>indexes help my query? In my case, I say NO because practically I got poor
>performance.
>2) If I get better performance by using join hints, is it wise to use them
>in SPs without letting the query optimizer to choose its own plan?
>Any help would be greatly appreciated.
You tell an interesting story.
Let's review what the optimizer is all about - no, let's go even
further and consider what optimization is all about! If you have two
tables and no indexes, any join requires the work of a full
cross-join. Optimization suggests you have an index at least on the
PK (SQLServer does that automatically, though it calls the PK a
constraint, only if you don't even have a PK do you really have no
indexes). Especially when building an index also includes keeping
statistics (as it usually does in SQLServer), an optimizer can then
decide if a particular query can use the index. In a lot of cases,
this will drop a thirty-second query to thirty milliseconds. It's
those big, fat wins that the *optimizer* is all about.
Now, it so happens that, if your query requires that some large
percentage of records have to be inspected, that the use of any index
becomes a tougher decision. In general, if your query requires more
than about a quarter of a table's records, it turns out to be cheaper
to scan the whole table than to use a (nonclustered) index (if the
records that need to be scanned fit a range in a *clustered* index,
you get other situations). What I'm trying to say here is that if
it's a relatively close decision, the optimizer can guess wrong. If
it's only a factor of 2x, I'd guess the optimizer will get it wrong
fairly often. The optimizer is about finding 10x and 100x wins, and
it's pretty good at that.
So, I'm guessing your test query involved a large percentage of your
total records, and your database was relatively small, maybe under
100mb or 1m records in the tables involved.
Another thing to try is to put a few more indexes on each table, so
that the optimizer has more statistics to work with. Just a single
index on the left table may not be enough for the optimizer. Would
the ITW tell you if more would help? I'm not sure, I've only played
with that, never tried to use it for real.
I think I've only found one case where I had to use a hint to improve
performance, and that was instead of adding another index, which might
have been a better solution. If you tell SQLServer about the PKs for
each table, and put indices on the obvious fields of the larger
tables, the optimizer is going to give you 90%++ of available
performance. Um, that assumes the database is reasonably normalized.
Further hand-tuning of queries, doing semantic splits of tables, using
materialized views, and tuning physical layouts, and other exotica,
are still there for really huge or really critical databases.
I really enjoy tuning SQLServer, I'm just getting into some of the
available tools and techniques, but so far, in the real world, it
seems what I really find on people's sites are the real basics, tables
with no indexes on heavily used fields, etc. The good news is I put
on the index, show a 99% improvement in time, and everybody is happy.
The bad news is I don't get to use all the fancy stuff.
HTH,
J.|||Thank you very much for your comprehensive explanation.
Actually I'm not too bad at optimization and I guess the problem is
something beyond optimization concepts.
ITW's suggestion was what I had done. The query cost was quite reasonable
after creating index and the execution plan improved. But the execution time
got worse. The clustered PK which helped plan, caused a slower query while
nonclustered PK made it faster.
I have to study more on two tables' structure to find out the reason.
Thanks again,
Leila
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:4e3fp0tq3g00vd7k9a64r73pd6ld6rarbe@.
4ax.com...
> On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
MyDB2[vbcol=seagreen]
> ...
which[vbcol=seagreen]
poor[vbcol=seagreen]
them[vbcol=seagreen]
> You tell an interesting story.
> Let's review what the optimizer is all about - no, let's go even
> further and consider what optimization is all about! If you have two
> tables and no indexes, any join requires the work of a full
> cross-join. Optimization suggests you have an index at least on the
> PK (SQLServer does that automatically, though it calls the PK a
> constraint, only if you don't even have a PK do you really have no
> indexes). Especially when building an index also includes keeping
> statistics (as it usually does in SQLServer), an optimizer can then
> decide if a particular query can use the index. In a lot of cases,
> this will drop a thirty-second query to thirty milliseconds. It's
> those big, fat wins that the *optimizer* is all about.
> Now, it so happens that, if your query requires that some large
> percentage of records have to be inspected, that the use of any index
> becomes a tougher decision. In general, if your query requires more
> than about a quarter of a table's records, it turns out to be cheaper
> to scan the whole table than to use a (nonclustered) index (if the
> records that need to be scanned fit a range in a *clustered* index,
> you get other situations). What I'm trying to say here is that if
> it's a relatively close decision, the optimizer can guess wrong. If
> it's only a factor of 2x, I'd guess the optimizer will get it wrong
> fairly often. The optimizer is about finding 10x and 100x wins, and
> it's pretty good at that.
> So, I'm guessing your test query involved a large percentage of your
> total records, and your database was relatively small, maybe under
> 100mb or 1m records in the tables involved.
> Another thing to try is to put a few more indexes on each table, so
> that the optimizer has more statistics to work with. Just a single
> index on the left table may not be enough for the optimizer. Would
> the ITW tell you if more would help? I'm not sure, I've only played
> with that, never tried to use it for real.
> I think I've only found one case where I had to use a hint to improve
> performance, and that was instead of adding another index, which might
> have been a better solution. If you tell SQLServer about the PKs for
> each table, and put indices on the obvious fields of the larger
> tables, the optimizer is going to give you 90%++ of available
> performance. Um, that assumes the database is reasonably normalized.
> Further hand-tuning of queries, doing semantic splits of tables, using
> materialized views, and tuning physical layouts, and other exotica,
> are still there for really huge or really critical databases.
> I really enjoy tuning SQLServer, I'm just getting into some of the
> available tools and techniques, but so far, in the real world, it
> seems what I really find on people's sites are the real basics, tables
> with no indexes on heavily used fields, etc. The good news is I put
> on the index, show a 99% improvement in time, and everybody is happy.
> The bad news is I don't get to use all the fancy stuff.
> HTH,
> J.
>
I have been given a database without any index to tune its indexes and
SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
create indexes in that and compare the results with SPs in MyDB1.
For a particular query (a LEFT JOIN), the execution plan showed that a table
scan(on parent table) and hash join was used. Therefore I created a PK on
parent table.
This time Query Optimizer used clustered index scan and nested loop and
totally the execution plan was quiet improved.
I used a single connection in Query Analyzer to run two copies of the query
on two different databases (MyDB1 without index, MyDB2 with index).
The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
far everything was as I expected.
But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
which had index, was slower. However Query Analyzer had indicated less
cost(12%)!
I turned Statistics Time on to make sure. Yes it was slower and took more
time to produce results.
I used ITW, it suggested exactly the same index as I had created. I
unchecked the Clustered option for the PK to make it nonclustered. A
bookmark lookup was added in execution plan but the query became very fast!
I tried to use different join hints and LEFT HASH JOIN improved query
performance again.
Please help me with these two questions:
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
Any help would be greatly appreciated.
Leila1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
It's usually not a bad guide.
It doesn't take into account what is in memory though - i.e. reading from
disk gives the same cost as reading from memory so if you have two queries o
n
the same data and the first gets all the data into memory then the first wil
l
take a lot longer but the cost won't take this into account.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
I would use hints sparingly. It's better to recode so that the plan the
system retrieves is reasonable - but sometimes you have no choice.
"Leila" wrote:
> Hi,
> I have been given a database without any index to tune its indexes and
> SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> create indexes in that and compare the results with SPs in MyDB1.
> For a particular query (a LEFT JOIN), the execution plan showed that a tab
le
> scan(on parent table) and hash join was used. Therefore I created a PK on
> parent table.
> This time Query Optimizer used clustered index scan and nested loop and
> totally the execution plan was quiet improved.
> I used a single connection in Query Analyzer to run two copies of the quer
y
> on two different databases (MyDB1 without index, MyDB2 with index).
> The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%.
So
> far everything was as I expected.
> But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB
2
> which had index, was slower. However Query Analyzer had indicated less
> cost(12%)!
> I turned Statistics Time on to make sure. Yes it was slower and took more
> time to produce results.
> I used ITW, it suggested exactly the same index as I had created. I
> unchecked the Clustered option for the PK to make it nonclustered. A
> bookmark lookup was added in execution plan but the query became very fast
!
> I tried to use different join hints and LEFT HASH JOIN improved query
> performance again.
> Please help me with these two questions:
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> Any help would be greatly appreciated.
> Leila
>
>|||Thanks Nigel,
I changed the order of executing two queries to make sure that the second
query is not always using buffer which first query has prepared. Besides, I
run DBCC FREEPROCCHACHE and DBCC DROPCLEANBUFFERS before each query. But the
result was the same always.
Do you know why clustered PK might have poor performance, but when I change
that to nonclustered it improves considerably?
Thanks,
Leila
"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:C6599BA9-1E50-41BE-A5F4-E15CAC597E84@.microsoft.com...
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> It's usually not a bad guide.
> It doesn't take into account what is in memory though - i.e. reading from
> disk gives the same cost as reading from memory so if you have two queries
on
> the same data and the first gets all the data into memory then the first
will[vbcol=seagreen]
> take a lot longer but the cost won't take this into account.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> I would use hints sparingly. It's better to recode so that the plan the
> system retrieves is reasonable - but sometimes you have no choice.
> "Leila" wrote:
>
table[vbcol=seagreen]
on[vbcol=seagreen]
query[vbcol=seagreen]
12%. So[vbcol=seagreen]
MyDB2[vbcol=seagreen]
more[vbcol=seagreen]
fast![vbcol=seagreen]
which[vbcol=seagreen]
poor[vbcol=seagreen]
them[vbcol=seagreen]|||On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>Do you know why clustered PK might have poor performance, but when I change
>that to nonclustered it improves considerably?
Hi Leila,
That can happen in some situations. To understand, you need to know what
exactly is stored on the leaf pages (the lowest level in the B-tree) of
each kind of index.
For the clustered index, the leaf pages contain all data from the table.
If the size of one row is 400 bytes, each clustered index leaf page will
contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
For a clustered index, the leaf pages contain the values of the indexed
columns plus a "row-locator" - usually the values of the clustered index'
columns; a physical row locator if there is no clustered index. If the
combined size of all columns in the index plus the row locator is 40
bytes, each nonclustered index leaf page will contain the information of
~200 rows ((8K - some overhead) / 40 bytes).
I think that your query used only columns that participate in the index
you used, but used a WHERE clause that prohibited the use of directly
finding the required rows in the index, so all rows had to be scanned. If
the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
index is clustered, but only 10 page reads if the index is nonclustered
(2000 / 200).
Remember: you'll lose this advantage as soon as you start referring to any
column that is not part of the index. The nonclustered index is cheaper
only because there is no need to locate and read the rest of the columns.
(This is called a "covering" index).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Hugo,
Thank you very much indeed! Maybe you're right and the problem is from the
pages' density. On Monday I will go and check. As far as I know, the
developers have used some TEXT and NCHAR data types that might cause the low
density in pages.
Thanks again!
Leila
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:c9vcp09a0hmttg6c9t4nha093190q52314@.
4ax.com...
> On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>
change[vbcol=seagreen]
> Hi Leila,
> That can happen in some situations. To understand, you need to know what
> exactly is stored on the leaf pages (the lowest level in the B-tree) of
> each kind of index.
> For the clustered index, the leaf pages contain all data from the table.
> If the size of one row is 400 bytes, each clustered index leaf page will
> contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
> For a clustered index, the leaf pages contain the values of the indexed
> columns plus a "row-locator" - usually the values of the clustered index'
> columns; a physical row locator if there is no clustered index. If the
> combined size of all columns in the index plus the row locator is 40
> bytes, each nonclustered index leaf page will contain the information of
> ~200 rows ((8K - some overhead) / 40 bytes).
> I think that your query used only columns that participate in the index
> you used, but used a WHERE clause that prohibited the use of directly
> finding the required rows in the index, so all rows had to be scanned. If
> the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
> index is clustered, but only 10 page reads if the index is nonclustered
> (2000 / 200).
> Remember: you'll lose this advantage as soon as you start referring to any
> column that is not part of the index. The nonclustered index is cheaper
> only because there is no need to locate and read the rest of the columns.
> (This is called a "covering" index).
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)|||On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
>But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
>which had index, was slower. However Query Analyzer had indicated less
>cost(12%)!
...
>I tried to use different join hints and LEFT HASH JOIN improved query
>performance again.
>Please help me with these two questions:
>1) Can I rely to Query Cost (relative to the batch) to discover that which
>indexes help my query? In my case, I say NO because practically I got poor
>performance.
>2) If I get better performance by using join hints, is it wise to use them
>in SPs without letting the query optimizer to choose its own plan?
>Any help would be greatly appreciated.
You tell an interesting story.
Let's review what the optimizer is all about - no, let's go even
further and consider what optimization is all about! If you have two
tables and no indexes, any join requires the work of a full
cross-join. Optimization suggests you have an index at least on the
PK (SQLServer does that automatically, though it calls the PK a
constraint, only if you don't even have a PK do you really have no
indexes). Especially when building an index also includes keeping
statistics (as it usually does in SQLServer), an optimizer can then
decide if a particular query can use the index. In a lot of cases,
this will drop a thirty-second query to thirty milliseconds. It's
those big, fat wins that the *optimizer* is all about.
Now, it so happens that, if your query requires that some large
percentage of records have to be inspected, that the use of any index
becomes a tougher decision. In general, if your query requires more
than about a quarter of a table's records, it turns out to be cheaper
to scan the whole table than to use a (nonclustered) index (if the
records that need to be scanned fit a range in a *clustered* index,
you get other situations). What I'm trying to say here is that if
it's a relatively close decision, the optimizer can guess wrong. If
it's only a factor of 2x, I'd guess the optimizer will get it wrong
fairly often. The optimizer is about finding 10x and 100x wins, and
it's pretty good at that.
So, I'm guessing your test query involved a large percentage of your
total records, and your database was relatively small, maybe under
100mb or 1m records in the tables involved.
Another thing to try is to put a few more indexes on each table, so
that the optimizer has more statistics to work with. Just a single
index on the left table may not be enough for the optimizer. Would
the ITW tell you if more would help? I'm not sure, I've only played
with that, never tried to use it for real.
I think I've only found one case where I had to use a hint to improve
performance, and that was instead of adding another index, which might
have been a better solution. If you tell SQLServer about the PKs for
each table, and put indices on the obvious fields of the larger
tables, the optimizer is going to give you 90%++ of available
performance. Um, that assumes the database is reasonably normalized.
Further hand-tuning of queries, doing semantic splits of tables, using
materialized views, and tuning physical layouts, and other exotica,
are still there for really huge or really critical databases.
I really enjoy tuning SQLServer, I'm just getting into some of the
available tools and techniques, but so far, in the real world, it
seems what I really find on people's sites are the real basics, tables
with no indexes on heavily used fields, etc. The good news is I put
on the index, show a 99% improvement in time, and everybody is happy.
The bad news is I don't get to use all the fancy stuff.
HTH,
J.|||Thank you very much for your comprehensive explanation.
Actually I'm not too bad at optimization and I guess the problem is
something beyond optimization concepts.
ITW's suggestion was what I had done. The query cost was quite reasonable
after creating index and the execution plan improved. But the execution time
got worse. The clustered PK which helped plan, caused a slower query while
nonclustered PK made it faster.
I have to study more on two tables' structure to find out the reason.
Thanks again,
Leila
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:4e3fp0tq3g00vd7k9a64r73pd6ld6rarbe@.
4ax.com...
> On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
MyDB2[vbcol=seagreen]
> ...
which[vbcol=seagreen]
poor[vbcol=seagreen]
them[vbcol=seagreen]
> You tell an interesting story.
> Let's review what the optimizer is all about - no, let's go even
> further and consider what optimization is all about! If you have two
> tables and no indexes, any join requires the work of a full
> cross-join. Optimization suggests you have an index at least on the
> PK (SQLServer does that automatically, though it calls the PK a
> constraint, only if you don't even have a PK do you really have no
> indexes). Especially when building an index also includes keeping
> statistics (as it usually does in SQLServer), an optimizer can then
> decide if a particular query can use the index. In a lot of cases,
> this will drop a thirty-second query to thirty milliseconds. It's
> those big, fat wins that the *optimizer* is all about.
> Now, it so happens that, if your query requires that some large
> percentage of records have to be inspected, that the use of any index
> becomes a tougher decision. In general, if your query requires more
> than about a quarter of a table's records, it turns out to be cheaper
> to scan the whole table than to use a (nonclustered) index (if the
> records that need to be scanned fit a range in a *clustered* index,
> you get other situations). What I'm trying to say here is that if
> it's a relatively close decision, the optimizer can guess wrong. If
> it's only a factor of 2x, I'd guess the optimizer will get it wrong
> fairly often. The optimizer is about finding 10x and 100x wins, and
> it's pretty good at that.
> So, I'm guessing your test query involved a large percentage of your
> total records, and your database was relatively small, maybe under
> 100mb or 1m records in the tables involved.
> Another thing to try is to put a few more indexes on each table, so
> that the optimizer has more statistics to work with. Just a single
> index on the left table may not be enough for the optimizer. Would
> the ITW tell you if more would help? I'm not sure, I've only played
> with that, never tried to use it for real.
> I think I've only found one case where I had to use a hint to improve
> performance, and that was instead of adding another index, which might
> have been a better solution. If you tell SQLServer about the PKs for
> each table, and put indices on the obvious fields of the larger
> tables, the optimizer is going to give you 90%++ of available
> performance. Um, that assumes the database is reasonably normalized.
> Further hand-tuning of queries, doing semantic splits of tables, using
> materialized views, and tuning physical layouts, and other exotica,
> are still there for really huge or really critical databases.
> I really enjoy tuning SQLServer, I'm just getting into some of the
> available tools and techniques, but so far, in the real world, it
> seems what I really find on people's sites are the real basics, tables
> with no indexes on heavily used fields, etc. The good news is I put
> on the index, show a 99% improvement in time, and everybody is happy.
> The bad news is I don't get to use all the fancy stuff.
> HTH,
> J.
>
Query Optimizer
Hi,
I have been given a database without any index to tune its indexes and
SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
create indexes in that and compare the results with SPs in MyDB1.
For a particular query (a LEFT JOIN), the execution plan showed that a table
scan(on parent table) and hash join was used. Therefore I created a PK on
parent table.
This time Query Optimizer used clustered index scan and nested loop and
totally the execution plan was quiet improved.
I used a single connection in Query Analyzer to run two copies of the query
on two different databases (MyDB1 without index, MyDB2 with index).
The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
far everything was as I expected.
But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
which had index, was slower. However Query Analyzer had indicated less
cost(12%)!
I turned Statistics Time on to make sure. Yes it was slower and took more
time to produce results.
I used ITW, it suggested exactly the same index as I had created. I
unchecked the Clustered option for the PK to make it nonclustered. A
bookmark lookup was added in execution plan but the query became very fast!
I tried to use different join hints and LEFT HASH JOIN improved query
performance again.
Please help me with these two questions:
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
Any help would be greatly appreciated.
Leila
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
It's usually not a bad guide.
It doesn't take into account what is in memory though - i.e. reading from
disk gives the same cost as reading from memory so if you have two queries on
the same data and the first gets all the data into memory then the first will
take a lot longer but the cost won't take this into account.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
I would use hints sparingly. It's better to recode so that the plan the
system retrieves is reasonable - but sometimes you have no choice.
"Leila" wrote:
> Hi,
> I have been given a database without any index to tune its indexes and
> SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> create indexes in that and compare the results with SPs in MyDB1.
> For a particular query (a LEFT JOIN), the execution plan showed that a table
> scan(on parent table) and hash join was used. Therefore I created a PK on
> parent table.
> This time Query Optimizer used clustered index scan and nested loop and
> totally the execution plan was quiet improved.
> I used a single connection in Query Analyzer to run two copies of the query
> on two different databases (MyDB1 without index, MyDB2 with index).
> The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
> far everything was as I expected.
> But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
> which had index, was slower. However Query Analyzer had indicated less
> cost(12%)!
> I turned Statistics Time on to make sure. Yes it was slower and took more
> time to produce results.
> I used ITW, it suggested exactly the same index as I had created. I
> unchecked the Clustered option for the PK to make it nonclustered. A
> bookmark lookup was added in execution plan but the query became very fast!
> I tried to use different join hints and LEFT HASH JOIN improved query
> performance again.
> Please help me with these two questions:
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> Any help would be greatly appreciated.
> Leila
>
>
|||Thanks Nigel,
I changed the order of executing two queries to make sure that the second
query is not always using buffer which first query has prepared. Besides, I
run DBCC FREEPROCCHACHE and DBCC DROPCLEANBUFFERS before each query. But the
result was the same always.
Do you know why clustered PK might have poor performance, but when I change
that to nonclustered it improves considerably?
Thanks,
Leila
"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:C6599BA9-1E50-41BE-A5F4-E15CAC597E84@.microsoft.com...
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> It's usually not a bad guide.
> It doesn't take into account what is in memory though - i.e. reading from
> disk gives the same cost as reading from memory so if you have two queries
on
> the same data and the first gets all the data into memory then the first
will[vbcol=seagreen]
> take a lot longer but the cost won't take this into account.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> I would use hints sparingly. It's better to recode so that the plan the
> system retrieves is reasonable - but sometimes you have no choice.
> "Leila" wrote:
table[vbcol=seagreen]
on[vbcol=seagreen]
query[vbcol=seagreen]
12%. So[vbcol=seagreen]
MyDB2[vbcol=seagreen]
more[vbcol=seagreen]
fast![vbcol=seagreen]
which[vbcol=seagreen]
poor[vbcol=seagreen]
them[vbcol=seagreen]
|||On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>Do you know why clustered PK might have poor performance, but when I change
>that to nonclustered it improves considerably?
Hi Leila,
That can happen in some situations. To understand, you need to know what
exactly is stored on the leaf pages (the lowest level in the B-tree) of
each kind of index.
For the clustered index, the leaf pages contain all data from the table.
If the size of one row is 400 bytes, each clustered index leaf page will
contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
For a clustered index, the leaf pages contain the values of the indexed
columns plus a "row-locator" - usually the values of the clustered index'
columns; a physical row locator if there is no clustered index. If the
combined size of all columns in the index plus the row locator is 40
bytes, each nonclustered index leaf page will contain the information of
~200 rows ((8K - some overhead) / 40 bytes).
I think that your query used only columns that participate in the index
you used, but used a WHERE clause that prohibited the use of directly
finding the required rows in the index, so all rows had to be scanned. If
the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
index is clustered, but only 10 page reads if the index is nonclustered
(2000 / 200).
Remember: you'll lose this advantage as soon as you start referring to any
column that is not part of the index. The nonclustered index is cheaper
only because there is no need to locate and read the rest of the columns.
(This is called a "covering" index).
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hugo,
Thank you very much indeed! Maybe you're right and the problem is from the
pages' density. On Monday I will go and check. As far as I know, the
developers have used some TEXT and NCHAR data types that might cause the low
density in pages.
Thanks again!
Leila
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:c9vcp09a0hmttg6c9t4nha093190q52314@.4ax.com... [vbcol=seagreen]
> On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
change
> Hi Leila,
> That can happen in some situations. To understand, you need to know what
> exactly is stored on the leaf pages (the lowest level in the B-tree) of
> each kind of index.
> For the clustered index, the leaf pages contain all data from the table.
> If the size of one row is 400 bytes, each clustered index leaf page will
> contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
> For a clustered index, the leaf pages contain the values of the indexed
> columns plus a "row-locator" - usually the values of the clustered index'
> columns; a physical row locator if there is no clustered index. If the
> combined size of all columns in the index plus the row locator is 40
> bytes, each nonclustered index leaf page will contain the information of
> ~200 rows ((8K - some overhead) / 40 bytes).
> I think that your query used only columns that participate in the index
> you used, but used a WHERE clause that prohibited the use of directly
> finding the required rows in the index, so all rows had to be scanned. If
> the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
> index is clustered, but only 10 page reads if the index is nonclustered
> (2000 / 200).
> Remember: you'll lose this advantage as soon as you start referring to any
> column that is not part of the index. The nonclustered index is cheaper
> only because there is no need to locate and read the rest of the columns.
> (This is called a "covering" index).
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
|||On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
>But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
>which had index, was slower. However Query Analyzer had indicated less
>cost(12%)!
...
>I tried to use different join hints and LEFT HASH JOIN improved query
>performance again.
>Please help me with these two questions:
>1) Can I rely to Query Cost (relative to the batch) to discover that which
>indexes help my query? In my case, I say NO because practically I got poor
>performance.
>2) If I get better performance by using join hints, is it wise to use them
>in SPs without letting the query optimizer to choose its own plan?
>Any help would be greatly appreciated.
You tell an interesting story.
Let's review what the optimizer is all about - no, let's go even
further and consider what optimization is all about! If you have two
tables and no indexes, any join requires the work of a full
cross-join. Optimization suggests you have an index at least on the
PK (SQLServer does that automatically, though it calls the PK a
constraint, only if you don't even have a PK do you really have no
indexes). Especially when building an index also includes keeping
statistics (as it usually does in SQLServer), an optimizer can then
decide if a particular query can use the index. In a lot of cases,
this will drop a thirty-second query to thirty milliseconds. It's
those big, fat wins that the *optimizer* is all about.
Now, it so happens that, if your query requires that some large
percentage of records have to be inspected, that the use of any index
becomes a tougher decision. In general, if your query requires more
than about a quarter of a table's records, it turns out to be cheaper
to scan the whole table than to use a (nonclustered) index (if the
records that need to be scanned fit a range in a *clustered* index,
you get other situations). What I'm trying to say here is that if
it's a relatively close decision, the optimizer can guess wrong. If
it's only a factor of 2x, I'd guess the optimizer will get it wrong
fairly often. The optimizer is about finding 10x and 100x wins, and
it's pretty good at that.
So, I'm guessing your test query involved a large percentage of your
total records, and your database was relatively small, maybe under
100mb or 1m records in the tables involved.
Another thing to try is to put a few more indexes on each table, so
that the optimizer has more statistics to work with. Just a single
index on the left table may not be enough for the optimizer. Would
the ITW tell you if more would help? I'm not sure, I've only played
with that, never tried to use it for real.
I think I've only found one case where I had to use a hint to improve
performance, and that was instead of adding another index, which might
have been a better solution. If you tell SQLServer about the PKs for
each table, and put indices on the obvious fields of the larger
tables, the optimizer is going to give you 90%++ of available
performance. Um, that assumes the database is reasonably normalized.
Further hand-tuning of queries, doing semantic splits of tables, using
materialized views, and tuning physical layouts, and other exotica,
are still there for really huge or really critical databases.
I really enjoy tuning SQLServer, I'm just getting into some of the
available tools and techniques, but so far, in the real world, it
seems what I really find on people's sites are the real basics, tables
with no indexes on heavily used fields, etc. The good news is I put
on the index, show a 99% improvement in time, and everybody is happy.
The bad news is I don't get to use all the fancy stuff.
HTH,
J.
|||Thank you very much for your comprehensive explanation.
Actually I'm not too bad at optimization and I guess the problem is
something beyond optimization concepts.
ITW's suggestion was what I had done. The query cost was quite reasonable
after creating index and the execution plan improved. But the execution time
got worse. The clustered PK which helped plan, caused a slower query while
nonclustered PK made it faster.
I have to study more on two tables' structure to find out the reason.
Thanks again,
Leila
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:4e3fp0tq3g00vd7k9a64r73pd6ld6rarbe@.4ax.com... [vbcol=seagreen]
> On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
MyDB2[vbcol=seagreen]
> ...
which[vbcol=seagreen]
poor[vbcol=seagreen]
them
> You tell an interesting story.
> Let's review what the optimizer is all about - no, let's go even
> further and consider what optimization is all about! If you have two
> tables and no indexes, any join requires the work of a full
> cross-join. Optimization suggests you have an index at least on the
> PK (SQLServer does that automatically, though it calls the PK a
> constraint, only if you don't even have a PK do you really have no
> indexes). Especially when building an index also includes keeping
> statistics (as it usually does in SQLServer), an optimizer can then
> decide if a particular query can use the index. In a lot of cases,
> this will drop a thirty-second query to thirty milliseconds. It's
> those big, fat wins that the *optimizer* is all about.
> Now, it so happens that, if your query requires that some large
> percentage of records have to be inspected, that the use of any index
> becomes a tougher decision. In general, if your query requires more
> than about a quarter of a table's records, it turns out to be cheaper
> to scan the whole table than to use a (nonclustered) index (if the
> records that need to be scanned fit a range in a *clustered* index,
> you get other situations). What I'm trying to say here is that if
> it's a relatively close decision, the optimizer can guess wrong. If
> it's only a factor of 2x, I'd guess the optimizer will get it wrong
> fairly often. The optimizer is about finding 10x and 100x wins, and
> it's pretty good at that.
> So, I'm guessing your test query involved a large percentage of your
> total records, and your database was relatively small, maybe under
> 100mb or 1m records in the tables involved.
> Another thing to try is to put a few more indexes on each table, so
> that the optimizer has more statistics to work with. Just a single
> index on the left table may not be enough for the optimizer. Would
> the ITW tell you if more would help? I'm not sure, I've only played
> with that, never tried to use it for real.
> I think I've only found one case where I had to use a hint to improve
> performance, and that was instead of adding another index, which might
> have been a better solution. If you tell SQLServer about the PKs for
> each table, and put indices on the obvious fields of the larger
> tables, the optimizer is going to give you 90%++ of available
> performance. Um, that assumes the database is reasonably normalized.
> Further hand-tuning of queries, doing semantic splits of tables, using
> materialized views, and tuning physical layouts, and other exotica,
> are still there for really huge or really critical databases.
> I really enjoy tuning SQLServer, I'm just getting into some of the
> available tools and techniques, but so far, in the real world, it
> seems what I really find on people's sites are the real basics, tables
> with no indexes on heavily used fields, etc. The good news is I put
> on the index, show a 99% improvement in time, and everybody is happy.
> The bad news is I don't get to use all the fancy stuff.
> HTH,
> J.
>
I have been given a database without any index to tune its indexes and
SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
create indexes in that and compare the results with SPs in MyDB1.
For a particular query (a LEFT JOIN), the execution plan showed that a table
scan(on parent table) and hash join was used. Therefore I created a PK on
parent table.
This time Query Optimizer used clustered index scan and nested loop and
totally the execution plan was quiet improved.
I used a single connection in Query Analyzer to run two copies of the query
on two different databases (MyDB1 without index, MyDB2 with index).
The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
far everything was as I expected.
But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
which had index, was slower. However Query Analyzer had indicated less
cost(12%)!
I turned Statistics Time on to make sure. Yes it was slower and took more
time to produce results.
I used ITW, it suggested exactly the same index as I had created. I
unchecked the Clustered option for the PK to make it nonclustered. A
bookmark lookup was added in execution plan but the query became very fast!
I tried to use different join hints and LEFT HASH JOIN improved query
performance again.
Please help me with these two questions:
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
Any help would be greatly appreciated.
Leila
1) Can I rely to Query Cost (relative to the batch) to discover that which
indexes help my query? In my case, I say NO because practically I got poor
performance.
It's usually not a bad guide.
It doesn't take into account what is in memory though - i.e. reading from
disk gives the same cost as reading from memory so if you have two queries on
the same data and the first gets all the data into memory then the first will
take a lot longer but the cost won't take this into account.
2) If I get better performance by using join hints, is it wise to use them
in SPs without letting the query optimizer to choose its own plan?
I would use hints sparingly. It's better to recode so that the plan the
system retrieves is reasonable - but sometimes you have no choice.
"Leila" wrote:
> Hi,
> I have been given a database without any index to tune its indexes and
> SPs(Let's say MyDB1). I made a copy of MyDB1 and attached it as MyDB2 to
> create indexes in that and compare the results with SPs in MyDB1.
> For a particular query (a LEFT JOIN), the execution plan showed that a table
> scan(on parent table) and hash join was used. Therefore I created a PK on
> parent table.
> This time Query Optimizer used clustered index scan and nested loop and
> totally the execution plan was quiet improved.
> I used a single connection in Query Analyzer to run two copies of the query
> on two different databases (MyDB1 without index, MyDB2 with index).
> The query cost (relative to the batch) on MyDB1 was 88% and on MyDB2 12%. So
> far everything was as I expected.
> But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
> which had index, was slower. However Query Analyzer had indicated less
> cost(12%)!
> I turned Statistics Time on to make sure. Yes it was slower and took more
> time to produce results.
> I used ITW, it suggested exactly the same index as I had created. I
> unchecked the Clustered option for the PK to make it nonclustered. A
> bookmark lookup was added in execution plan but the query became very fast!
> I tried to use different join hints and LEFT HASH JOIN improved query
> performance again.
> Please help me with these two questions:
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> Any help would be greatly appreciated.
> Leila
>
>
|||Thanks Nigel,
I changed the order of executing two queries to make sure that the second
query is not always using buffer which first query has prepared. Besides, I
run DBCC FREEPROCCHACHE and DBCC DROPCLEANBUFFERS before each query. But the
result was the same always.
Do you know why clustered PK might have poor performance, but when I change
that to nonclustered it improves considerably?
Thanks,
Leila
"Nigel Rivett" <sqlnr@.hotmail.com> wrote in message
news:C6599BA9-1E50-41BE-A5F4-E15CAC597E84@.microsoft.com...
> 1) Can I rely to Query Cost (relative to the batch) to discover that which
> indexes help my query? In my case, I say NO because practically I got poor
> performance.
> It's usually not a bad guide.
> It doesn't take into account what is in memory though - i.e. reading from
> disk gives the same cost as reading from memory so if you have two queries
on
> the same data and the first gets all the data into memory then the first
will[vbcol=seagreen]
> take a lot longer but the cost won't take this into account.
> 2) If I get better performance by using join hints, is it wise to use them
> in SPs without letting the query optimizer to choose its own plan?
> I would use hints sparingly. It's better to recode so that the plan the
> system retrieves is reasonable - but sometimes you have no choice.
> "Leila" wrote:
table[vbcol=seagreen]
on[vbcol=seagreen]
query[vbcol=seagreen]
12%. So[vbcol=seagreen]
MyDB2[vbcol=seagreen]
more[vbcol=seagreen]
fast![vbcol=seagreen]
which[vbcol=seagreen]
poor[vbcol=seagreen]
them[vbcol=seagreen]
|||On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
>Do you know why clustered PK might have poor performance, but when I change
>that to nonclustered it improves considerably?
Hi Leila,
That can happen in some situations. To understand, you need to know what
exactly is stored on the leaf pages (the lowest level in the B-tree) of
each kind of index.
For the clustered index, the leaf pages contain all data from the table.
If the size of one row is 400 bytes, each clustered index leaf page will
contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
For a clustered index, the leaf pages contain the values of the indexed
columns plus a "row-locator" - usually the values of the clustered index'
columns; a physical row locator if there is no clustered index. If the
combined size of all columns in the index plus the row locator is 40
bytes, each nonclustered index leaf page will contain the information of
~200 rows ((8K - some overhead) / 40 bytes).
I think that your query used only columns that participate in the index
you used, but used a WHERE clause that prohibited the use of directly
finding the required rows in the index, so all rows had to be scanned. If
the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
index is clustered, but only 10 page reads if the index is nonclustered
(2000 / 200).
Remember: you'll lose this advantage as soon as you start referring to any
column that is not part of the index. The nonclustered index is cheaper
only because there is no need to locate and read the rest of the columns.
(This is called a "covering" index).
Best, Hugo
(Remove _NO_ and _SPAM_ to get my e-mail address)
|||Hugo,
Thank you very much indeed! Maybe you're right and the problem is from the
pages' density. On Monday I will go and check. As far as I know, the
developers have used some TEXT and NCHAR data types that might cause the low
density in pages.
Thanks again!
Leila
"Hugo Kornelis" <hugo@.pe_NO_rFact.in_SPAM_fo> wrote in message
news:c9vcp09a0hmttg6c9t4nha093190q52314@.4ax.com... [vbcol=seagreen]
> On Sat, 13 Nov 2004 21:20:28 +0330, Leila wrote:
change
> Hi Leila,
> That can happen in some situations. To understand, you need to know what
> exactly is stored on the leaf pages (the lowest level in the B-tree) of
> each kind of index.
> For the clustered index, the leaf pages contain all data from the table.
> If the size of one row is 400 bytes, each clustered index leaf page will
> contain the information of ~20 rows ((8K - some overhead) / 400 bytes).
> For a clustered index, the leaf pages contain the values of the indexed
> columns plus a "row-locator" - usually the values of the clustered index'
> columns; a physical row locator if there is no clustered index. If the
> combined size of all columns in the index plus the row locator is 40
> bytes, each nonclustered index leaf page will contain the information of
> ~200 rows ((8K - some overhead) / 40 bytes).
> I think that your query used only columns that participate in the index
> you used, but used a WHERE clause that prohibited the use of directly
> finding the required rows in the index, so all rows had to be scanned. If
> the table has 2000 rows, this would take 100 page reads (2000 / 20) if the
> index is clustered, but only 10 page reads if the index is nonclustered
> (2000 / 200).
> Remember: you'll lose this advantage as soon as you start referring to any
> column that is not part of the index. The nonclustered index is cheaper
> only because there is no need to locate and read the rest of the columns.
> (This is called a "covering" index).
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
|||On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
>But when I run query on MyDB1 and MyDB2 several times, I noticed that MyDB2
>which had index, was slower. However Query Analyzer had indicated less
>cost(12%)!
...
>I tried to use different join hints and LEFT HASH JOIN improved query
>performance again.
>Please help me with these two questions:
>1) Can I rely to Query Cost (relative to the batch) to discover that which
>indexes help my query? In my case, I say NO because practically I got poor
>performance.
>2) If I get better performance by using join hints, is it wise to use them
>in SPs without letting the query optimizer to choose its own plan?
>Any help would be greatly appreciated.
You tell an interesting story.
Let's review what the optimizer is all about - no, let's go even
further and consider what optimization is all about! If you have two
tables and no indexes, any join requires the work of a full
cross-join. Optimization suggests you have an index at least on the
PK (SQLServer does that automatically, though it calls the PK a
constraint, only if you don't even have a PK do you really have no
indexes). Especially when building an index also includes keeping
statistics (as it usually does in SQLServer), an optimizer can then
decide if a particular query can use the index. In a lot of cases,
this will drop a thirty-second query to thirty milliseconds. It's
those big, fat wins that the *optimizer* is all about.
Now, it so happens that, if your query requires that some large
percentage of records have to be inspected, that the use of any index
becomes a tougher decision. In general, if your query requires more
than about a quarter of a table's records, it turns out to be cheaper
to scan the whole table than to use a (nonclustered) index (if the
records that need to be scanned fit a range in a *clustered* index,
you get other situations). What I'm trying to say here is that if
it's a relatively close decision, the optimizer can guess wrong. If
it's only a factor of 2x, I'd guess the optimizer will get it wrong
fairly often. The optimizer is about finding 10x and 100x wins, and
it's pretty good at that.
So, I'm guessing your test query involved a large percentage of your
total records, and your database was relatively small, maybe under
100mb or 1m records in the tables involved.
Another thing to try is to put a few more indexes on each table, so
that the optimizer has more statistics to work with. Just a single
index on the left table may not be enough for the optimizer. Would
the ITW tell you if more would help? I'm not sure, I've only played
with that, never tried to use it for real.
I think I've only found one case where I had to use a hint to improve
performance, and that was instead of adding another index, which might
have been a better solution. If you tell SQLServer about the PKs for
each table, and put indices on the obvious fields of the larger
tables, the optimizer is going to give you 90%++ of available
performance. Um, that assumes the database is reasonably normalized.
Further hand-tuning of queries, doing semantic splits of tables, using
materialized views, and tuning physical layouts, and other exotica,
are still there for really huge or really critical databases.
I really enjoy tuning SQLServer, I'm just getting into some of the
available tools and techniques, but so far, in the real world, it
seems what I really find on people's sites are the real basics, tables
with no indexes on heavily used fields, etc. The good news is I put
on the index, show a 99% improvement in time, and everybody is happy.
The bad news is I don't get to use all the fancy stuff.
HTH,
J.
|||Thank you very much for your comprehensive explanation.
Actually I'm not too bad at optimization and I guess the problem is
something beyond optimization concepts.
ITW's suggestion was what I had done. The query cost was quite reasonable
after creating index and the execution plan improved. But the execution time
got worse. The clustered PK which helped plan, caused a slower query while
nonclustered PK made it faster.
I have to study more on two tables' structure to find out the reason.
Thanks again,
Leila
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:4e3fp0tq3g00vd7k9a64r73pd6ld6rarbe@.4ax.com... [vbcol=seagreen]
> On Sat, 13 Nov 2004 15:58:36 +0330, "Leila" <leilas@.hotpop.com> wrote:
MyDB2[vbcol=seagreen]
> ...
which[vbcol=seagreen]
poor[vbcol=seagreen]
them
> You tell an interesting story.
> Let's review what the optimizer is all about - no, let's go even
> further and consider what optimization is all about! If you have two
> tables and no indexes, any join requires the work of a full
> cross-join. Optimization suggests you have an index at least on the
> PK (SQLServer does that automatically, though it calls the PK a
> constraint, only if you don't even have a PK do you really have no
> indexes). Especially when building an index also includes keeping
> statistics (as it usually does in SQLServer), an optimizer can then
> decide if a particular query can use the index. In a lot of cases,
> this will drop a thirty-second query to thirty milliseconds. It's
> those big, fat wins that the *optimizer* is all about.
> Now, it so happens that, if your query requires that some large
> percentage of records have to be inspected, that the use of any index
> becomes a tougher decision. In general, if your query requires more
> than about a quarter of a table's records, it turns out to be cheaper
> to scan the whole table than to use a (nonclustered) index (if the
> records that need to be scanned fit a range in a *clustered* index,
> you get other situations). What I'm trying to say here is that if
> it's a relatively close decision, the optimizer can guess wrong. If
> it's only a factor of 2x, I'd guess the optimizer will get it wrong
> fairly often. The optimizer is about finding 10x and 100x wins, and
> it's pretty good at that.
> So, I'm guessing your test query involved a large percentage of your
> total records, and your database was relatively small, maybe under
> 100mb or 1m records in the tables involved.
> Another thing to try is to put a few more indexes on each table, so
> that the optimizer has more statistics to work with. Just a single
> index on the left table may not be enough for the optimizer. Would
> the ITW tell you if more would help? I'm not sure, I've only played
> with that, never tried to use it for real.
> I think I've only found one case where I had to use a hint to improve
> performance, and that was instead of adding another index, which might
> have been a better solution. If you tell SQLServer about the PKs for
> each table, and put indices on the obvious fields of the larger
> tables, the optimizer is going to give you 90%++ of available
> performance. Um, that assumes the database is reasonably normalized.
> Further hand-tuning of queries, doing semantic splits of tables, using
> materialized views, and tuning physical layouts, and other exotica,
> are still there for really huge or really critical databases.
> I really enjoy tuning SQLServer, I'm just getting into some of the
> available tools and techniques, but so far, in the real world, it
> seems what I really find on people's sites are the real basics, tables
> with no indexes on heavily used fields, etc. The good news is I put
> on the index, show a 99% improvement in time, and everybody is happy.
> The bad news is I don't get to use all the fancy stuff.
> HTH,
> J.
>
Subscribe to:
Posts (Atom)