Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Need help with hint syntax in SQL Server 2000

 
   Database Help (Home) -> General Discussions RSS
Next:  License problems (and coredump) after Fixpack  
Author Message
Mark D Powell

External


Since: Dec 20, 2007
Posts: 294



(Msg. 1) Posted: Fri Dec 05, 2008 1:48 pm
Post subject: Need help with hint syntax in SQL Server 2000
Archived from groups: comp>databases>ms-sqlserver (more info?)

I have only used hints a few times and it has been a while. Every
time I try to play with an SQL statement to force use of index over a
non-indexed path so I can compare the two approaches it take me
forever because I cannot remember how to code the hints. I just
cannot figure it out form the books on-line syntax diagrams.

Can someone point to to an article which has numerous examples of how
to code the hints into the SQL?

It will probably eventually clink since it has in the past, but I
would rather not spend a couple hours on whatever stupid mistake I am
making.

Thanks
-- Mark D Powell --

 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Mark D Powell

External


Since: Dec 20, 2007
Posts: 294



(Msg. 2) Posted: Sat Dec 06, 2008 7:39 am
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 6, 8:10 am, Erland Sommarskog wrote:
> Mark D Powell (Mark.Pow...@eds.com) writes:
>
> > I have only used hints a few times and it has been a while.  Every
> > time I try to play with an SQL statement to force use of index over a
> > non-indexed path so I can compare the two approaches it take me
> > forever because I cannot remember how to code the hints.  I just
> > cannot figure it out form the books on-line syntax diagrams.
>
> > Can someone point to to an article which has numerous examples of how
> > to code the hints into the SQL?
>
> > It will probably eventually clink since it has in the past, but I
> > would rather not spend a couple hours on whatever stupid mistake I am
> > making.
>
>    FROM tbl alias WITH (INDEX = yourindex)
>
> or
>
>    FROM tbl WITH (INDEX = yourindex)
>
> if you don't use aliases.
>
> The WITH keyword is not mandatory in SQL 2000, but it is SQL 2005, so start
> using it.
>
> If you want to force the clustered index, I think it's better to say:
>
>    FROM tbl WITH (INDEX = 1)
>
> as a name easily could to wrong. But for non-clustered indexes, names is
> better over index ids.
>
> There are some older syntax that you have used in the past. Forget about
> those.
>
> --
> Erland Sommarskog, SQL Server MVP, esq....DeleteThis@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000:http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Blast. Based on < table_hint > ::=
{ INDEX ( index_val [ ,...n ] )
I was doing: with index (index_name)

I will have to the correct syntax on Monday and see if I can get the
optimizer to try the index. It is a simple query with one condition on
a view that is a union of two joins. The view is written using the
new with ANSI 92 join syntax. We scan a small 138 row table and then
scan a 5.9M row tables. I am trying to index the column the join is
done on and a second column that is used to filter the large table for
a time test. I haven't worked out how selective the index will be yet
as I have to present proof that an index would not fix the problem. I
have suggested purging as an alternate if I cannot find an index that
will help.

I know a FROM (table) hint on a query against a view is propogated to
the tables in the view but will an index hint propogate or will I have
to place the hint into the view itself? I have already created a copy
of the real view to use in my testing but I ran into the syntax error
again so I haven't had the opportunity to see if the hint propogates
or now.

Thank you for the reply.
-- Mark D Powell --

 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 3) Posted: Sat Dec 06, 2008 9:28 am
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mark D Powell (Mark.Powell@eds.com) writes:
> I have only used hints a few times and it has been a while. Every
> time I try to play with an SQL statement to force use of index over a
> non-indexed path so I can compare the two approaches it take me
> forever because I cannot remember how to code the hints. I just
> cannot figure it out form the books on-line syntax diagrams.
>
> Can someone point to to an article which has numerous examples of how
> to code the hints into the SQL?
>
> It will probably eventually clink since it has in the past, but I
> would rather not spend a couple hours on whatever stupid mistake I am
> making.

FROM tbl alias WITH (INDEX = yourindex)

or

FROM tbl WITH (INDEX = yourindex)

if you don't use aliases.

The WITH keyword is not mandatory in SQL 2000, but it is SQL 2005, so start
using it.

If you want to force the clustered index, I think it's better to say:

FROM tbl WITH (INDEX = 1)

as a name easily could to wrong. But for non-clustered indexes, names is
better over index ids.

There are some older syntax that you have used in the past. Forget about
those.

--
Erland Sommarskog, SQL Server MVP, esquel.RemoveThis@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 4) Posted: Sat Dec 06, 2008 11:25 am
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mark D Powell (Mark.Powell@eds.com) writes:
> Blast. Based on < table_hint > ::=
> { INDEX ( index_val [ ,...n ] )
> I was doing: with index (index_name)

There are just too many ways to specify index hints!

What you missed was the extra layer of parenthesis, found higher up
in the topic. This is what you should try:

WITH (INDEX (myindex))

I see that the syntax I suggested is not in Books Online 2000, but it
appears in Books Online 2008, although it seem to suggest that I should
say:

WITH (INDEX = (myindex))

Nevertheless, I am quite sure that my original proposal works.

> I know a FROM (table) hint on a query against a view is propogated to
> the tables in the view but will an index hint propogate or will I have
> to place the hint into the view itself?

I think you will have to stick the hint in the view itself. Which is
something I would try to avoid. But try using the query as such with
the hint and see if it helps.

But run UPDATE STATISTICS WITH FULLSCAN on the tables first.


--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Mark D Powell

External


Since: Dec 20, 2007
Posts: 294



(Msg. 5) Posted: Tue Dec 09, 2008 8:22 am
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 6, 11:00 am, Erland Sommarskog wrote:
> Mark D Powell (Mark.Pow...@eds.com) writes:
>
> > Blast.  Based on < table_hint > ::=
> >     { INDEX ( index_val [ ,...n ] )
> > I was doing:  with index (index_name)
>
> There are just too many ways to specify index hints!
>
> What you missed was the extra layer of parenthesis, found higher up
> in the topic. This is what you should try:
>
>    WITH (INDEX (myindex))
>
> I see that the syntax I suggested is not in Books Online 2000, but it
> appears in Books Online 2008, although it seem to suggest that I should
> say:
>
>    WITH (INDEX = (myindex))
>
> Nevertheless, I am quite sure that my original proposal works.
>
> > I know a FROM (table) hint on a query against a view is propogated to
> > the tables in the view but will an index hint propogate or will I have
> > to place the hint into the view itself?  
>
> I think you will have to stick the hint in the view itself. Which is
> something I would try to avoid. But try using the query as such with
> the hint and see if it helps.
>
> But run UPDATE STATISTICS WITH FULLSCAN on the tables first.
>
> --
> Erland Sommarskog, SQL Server MVP, esq... RemoveThis @sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000:http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Thank you for the update. I did not get to work on this yesterday but
I intend to try to test today.

-- Mark D Powell --
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Mark D Powell

External


Since: Dec 20, 2007
Posts: 294



(Msg. 6) Posted: Mon Dec 15, 2008 12:16 pm
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 9, 11:22 am, Mark D Powell wrote:
> On Dec 6, 11:00 am, Erland Sommarskog wrote:
>
>
>
>
>
> > Mark D Powell (Mark.Pow...@eds.com) writes:
>
> > > Blast.  Based on < table_hint > ::=
> > >     { INDEX ( index_val [ ,...n ] )
> > > I was doing:  with index (index_name)
>
> > There are just too many ways to specify index hints!
>
> > What you missed was the extra layer of parenthesis, found higher up
> > in the topic. This is what you should try:
>
> >    WITH (INDEX (myindex))
>
> > I see that the syntax I suggested is not in Books Online 2000, but it
> > appears in Books Online 2008, although it seem to suggest that I should
> > say:
>
> >    WITH (INDEX = (myindex))
>
> > Nevertheless, I am quite sure that my original proposal works.
>
> > > I know a FROM (table) hint on a query against a view is propogated to
> > > the tables in the view but will an index hint propogate or will I have
> > > to place the hint into the view itself?  
>
> > I think you will have to stick the hint in the view itself. Which is
> > something I would try to avoid. But try using the query as such with
> > the hint and see if it helps.
>
> > But run UPDATE STATISTICS WITH FULLSCAN on the tables first.
>
> > --
> > Erland Sommarskog, SQL Server MVP, esq....DeleteThis@sommarskog.se
>
> > Links for SQL Server Books Online:
> > SQL 2008:http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> > SQL 2005:http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> > SQL 2000:http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
> Thank you for the update.  I did not get to work on this yesterday but
> I intend to try to test today.
>
> -- Mark D Powell --- Hide quoted text -
>
> - Show quoted text -

Update. I figured out what was wrong and got around the issue but I
think I should have been able to do it differently.

The query was a simple query against a view:
select * from view where col = value

This value was indexed in the primary target table under the view with
5.4M rows. The view is a union all of a two table join to a multi-
table join.

When I coded the index hint the SQL Server plan showed it being used
however a test of the query took 9 minutes instead of the 1 being
taken in the full table scan. A review of the statistics seemed to
indicate that although the index was used to retrieve the data the
index value availalbe in the query was not being applied as all 5.4M
rows were being passed up the plan.

A quick query agains the large table for a count showed only 205 rows
for the value in question and a test query ran immediately to fetch
those rows via the index. I tried coding a view on this table and
using it in a new version of the existing view to get SQL Server to
filter via the index. No luck.

So I took the modified view code I was using and placed the filter
condition into the sql in both parts of the union and the result was
instanct. I gave that to the developer. He was happy enough with the
result.

It seems like SQL Server should have pushed the filter condition down
and performed it against the large table for the query against the
view like it does for the extracted view SQL when submitted as a
query.

I saw the noexpand hint but that seems the opposite of what I think
should have happened. Anyone able to explain why SQL Server was
unable to recognize that it shoul push the where clause condition down
to the table access?

-- Mark D Powell --
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Hugo Kornelis

External


Since: Jan 11, 2008
Posts: 440



(Msg. 7) Posted: Mon Dec 15, 2008 5:25 pm
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 15 Dec 2008 12:16:40 -0800 (PST), Mark D Powell wrote:

> Anyone able to explain why SQL Server was
>unable to recognize that it shoul push the where clause condition down
>to the table access?

Hi Mark,

Based on your very generic description, the only thing I can say is that
I would first check if the UNION ALL has something to do with it.

For more specific comments, though, we need more information. For
starters, we need the design of all tables and views involved (posted as
CREATE TABLE and CREATE VIEW statements, including all properties,
constraints, and indexes). Some sample data might help as well, though
in this case I would definitely want to see an execution plan (captured
with SET SHOWPLAN_ALL ON) and information about numbers of matching rows
in the various tables.

You can of course replace table and column names to protect sensitive
data, and you can also (in fact, I even encourage you to) simplify the
problem as far as possible.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 8) Posted: Tue Dec 16, 2008 5:25 pm
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Mark D Powell (Mark.Powell@eds.com) writes:
> This value was indexed in the primary target table under the view with
> 5.4M rows. The view is a union all of a two table join to a multi-
> table join.
>...
> It seems like SQL Server should have pushed the filter condition down
> and performed it against the large table for the query against the
> view like it does for the extracted view SQL when submitted as a
> query.
>
> I saw the noexpand hint but that seems the opposite of what I think
> should have happened. Anyone able to explain why SQL Server was
> unable to recognize that it shoul push the where clause condition down
> to the table access?

There are just too many unknowns here. My primary guess it that your
view has some condition that leads to implicit conversion, and thus
preventing an index seek.

The NOEXPAND hint applies to indexed views, which your obviously
isn't. (UNION ALL is not permitted in indexed views.)


--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Mark D Powell

External


Since: Dec 20, 2007
Posts: 294



(Msg. 9) Posted: Wed Dec 17, 2008 5:54 am
Post subject: Re: Need help with hint syntax in SQL Server 2000 [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Dec 15, 5:16 pm, Hugo Kornelis
wrote:
> On Mon, 15 Dec 2008 12:16:40 -0800 (PST), Mark D Powell wrote:
> > Anyone able to explain why SQL Server was
> >unable to recognize that it shoul push the where clause condition down
> >to the table access?
>
> Hi Mark,
>
> Based on your very generic description, the only thing I can say is that
> I would first check if the UNION ALL has something to do with it.
>
> For more specific comments, though, we need more information. For
> starters, we need the design of all tables and views involved (posted as
> CREATE TABLE and CREATE VIEW statements, including all properties,
> constraints, and indexes). Some sample data might help as well, though
> in this case I would definitely want to see an execution plan (captured
> with SET SHOWPLAN_ALL ON) and information about numbers of matching rows
> in the various tables.
>
> You can of course replace table and column names to protect sensitive
> data, and you can also (in fact, I even encourage you to) simplify the
> problem as far as possible.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog:http://sqlblog.com/blogs/hugo_kornelis

Thank you both for the replies. With the holidays I do not know if I
will be able to devote some time to trying to figure out what stopped
SQL Server from pushing the filter condition down but from the replies
I take it that normally it can do so. We do not have very many
complex views in use in our sytem so this is the first time I have
encountered a proble like this. Tryng to test a version of the view
with the union all removed should not be time consuming and I do not
remember if I looked for a difference in data type definitions. I can
try to double check that.

-- Mark D Powell --
 >> Stay informed about: Need help with hint syntax in SQL Server 2000 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Can Analysis Server and Report Server run on SQL Server 20.. - I have installed Analysis Server (AS) and Reporting Services (RS) on SQL Server 2000 before. Now I have been asked to install these on a failover Cluster we have. Previously I just ran the SQL Server 2000 install program off our network disk and then..

Installing sql server 2000 on windows server 2008 - We are migrating a windows 2003 server machine to windows server 2008. Can we still use sql server 2000 on it? I know it is unsupported, but sql 2000 is doing fine right now and don't want to spend the extra cash

Queries Log on SQL Server 2000 ??? - Is there any log that catches all querys in SQL Server 2000??? Full problem (in slanish): http://foro.hackhispano.com/showthread.php?t=28980 Thanks

Access 2000 and SQL Server 2005 - Is it possible to connect to Sql Server 2005 using an Access 2000 adp? I keep trying, but it doesn't want to accept my id, even though it is right.

SQL Server 2000 SP4 Becomes clogged!!! - I have a SQL Server 2000 SP4 Enterprise Edition with Build 8.00.2039. OS: Microsoft Windows Server 2003 Ent. SP2. 6GB Ram and AWE is enabled for SQL Server to use more than 4 GB. The server hosts some reporting databases where Business Objects execute....
   Database Help (Home) -> General Discussions All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]