Showing posts with label queries. Show all posts
Showing posts with label queries. Show all posts

Friday, March 30, 2012

Informational Queries

Hello,
Are there any commands that would give infomation on all DBs on a SQL
server? (IE size, location of files, name, that sort of thing?) I have just
been given about twenty SQLservers and due to a person leaving I am hoping to
find ot some information about what is on them
I apprecitate the help.
jj
declare @.dbname sysname
if object_id('tempdb..#dbs')is not null drop table #dbs
select
[name]
into
#dbs
from
master..sysdatabases
while (select count(*) from #dbs)>0 begin
select top 1 @.dbname=[name] from #dbs
exec ('sp_helpdb [' + @.dbname + ']')
delete #dbs where [name]=@.dbname
end
regards,
Mark Baekdal
http://www.dbghost.com
http://www.innovartis.co.uk
+44 (0)208 241 1762
Build, Comparison and Synchronization from Source Control = Database change
management for SQL Server
"John Jarrett" wrote:

> Hello,
> Are there any commands that would give infomation on all DBs on a SQL
> server? (IE size, location of files, name, that sort of thing?) I have just
> been given about twenty SQLservers and due to a person leaving I am hoping to
> find ot some information about what is on them
> I apprecitate the help.
> --
> jj
|||The system tables will provide you with the appropiate information, but....
the best thing is to query them via the predefined stored procedures, read
about the sp_help% procedures in BOL, thatll help you.
For example sp_helpdb, sp_helpfiles
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
"John Jarrett" <JohnJarrett@.discussions.microsoft.com> schrieb im
Newsbeitrag news:11569D6F-1381-4E14-9F1E-C0EFB2ABA943@.microsoft.com...
> Hello,
> Are there any commands that would give infomation on all DBs on a SQL
> server? (IE size, location of files, name, that sort of thing?) I have
> just
> been given about twenty SQLservers and due to a person leaving I am hoping
> to
> find ot some information about what is on them
> I apprecitate the help.
> --
> jj
|||See sps sp_helpdb and sp_helpfile in BOL.
AMB
"John Jarrett" wrote:

> Hello,
> Are there any commands that would give infomation on all DBs on a SQL
> server? (IE size, location of files, name, that sort of thing?) I have just
> been given about twenty SQLservers and due to a person leaving I am hoping to
> find ot some information about what is on them
> I apprecitate the help.
> --
> jj
sql

INFORMATION_SCHEMA Information

I can use queries to retreive many informations from INFORMATION_SCHEMA
SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'tablename'
But I want to know how to find where that table exists as it is not
part of sysobjects
MadhivananThe Information Schema consists of views, which you can find in the Master
database.
David Portas
SQL Server MVP
--sql

Monday, March 26, 2012

Info Recursive Queries

Hello all,

for computig graphs and its transitive closure I need recursive SQL-Queries. Best it will be fast.

Now I heard of a new syntax in SQL: the WITH RECURSIVE-Clause.

Which Database-Editions and versions support those recursive queries?

Can anyone tell me where can I find more info about it?

Thanks and best regards

yelmin

It is implemented in SQL Server 2005 with the new CTE ( common table expression ) the base requirements your tables must be UNION compatible by default or through conversion because the sections are connected by UNION ALL. It is very complex if you need to go many levels deep you may want to check out Joe Celko's Trees book. Hope this helps.

Microsoft docs to get you started.

http://msdn2.microsoft.com/en-us/library/ms186243.aspx

http://msdn2.microsoft.com/en-us/library/ms175972.aspx

The book to browse at your local book store.

http://www.amazon.com/Hierarchies-Smarties-Kaufmann-Management-Systems/dp/1558609202

Friday, March 23, 2012

Inexplicable Invalid Syntax error

I created a DB in Access.. then I converted it to work with SQL Server. I have several tables and queries that are working just fine with it.

However, the other day I made a few changes to one of the tables ( Order table), and it seems no matter what kind of query I do with it, it always returns this error:

Invalid syntax near the keyword 'Order'

The query is IDENTICAL to about 10 others like it.. but no matter what kind of query I do to this Order table.. it always returns the same error. I've stepped through it, looked at the query builder, and there's nothing wrong with the format. Here is what the code looks like..

strSQL = "SELECT OrderId FROM Order WHERE SessionId = '"
+ "testfudgeddata" + "' ";
cm.CommandText = strSQL;
dr = cm.ExecuteReader();

while( dr.Read() )
{
orderId = dr.GetInt32(0);
}

It always fails when ExecuteReader() gets called.. with the same error. The syntax is exactly the same as many other queries.. and I even typed in 'testfudgeddata' into the db directly. I am thinking this problem might be related to some proxy-type information that I need to clean out due to the change of the name of this table since it was created in Access.. anyone ever see anything like this?

BrentORDER is a reserved word in SQL Server. change the table name (!)|||Try placing the table name in square brackets.|||AAAAK!

It seems every time I ask for help, I figure out the problem right after. I ran Query Analyzer and was a little confused why my Order table text was blue.. and the other tables were black.

Order is KEYWORD!! DUH!!

Anyways, it's a little funny I guess.. hope you can all get a good laugh from my misery.. heh|||Thanks for the reply guys.. looks like all 3 of us posted at exactly the same time. ;)sql

Wednesday, March 21, 2012

Indexing..... Which one will use....

I have a table with the following columns.
Customer Id, SSN and rest of the columns.
I have two queries, one with WHERE criteria as CustomerId and other with
WHERE criteria as SSN.
I have created two Index on the above table. One index uses CustomerId as
the indexed column and other index uses SSN as the Index column.
So when I run the queries how to find out that SQL Server used the
appropriate index. Is it possible to specify the index while running the
query through the application?
Thanks,
AkshayRun your query by enabling Execution plan in Query Analyzer
Thanks,
RK
"Akshay Sen" wrote:

> I have a table with the following columns.
> Customer Id, SSN and rest of the columns.
> I have two queries, one with WHERE criteria as CustomerId and other with
> WHERE criteria as SSN.
> I have created two Index on the above table. One index uses CustomerId as
> the indexed column and other index uses SSN as the Index column.
> So when I run the queries how to find out that SQL Server used the
> appropriate index. Is it possible to specify the index while running the
> query through the application?
> Thanks,
> Akshay
>
>
>
>|||and as for the second one: yes, you can specify which index to be used, via
a hint:
select ... from table with(index(index_name))
but you should better leave to sql server to determine the execution plan,
and the indexes used. sql server's reasoning is based on number of factors,
like column and index statistics, which change over time and which your
application doesn't know about. even worse, the execution plan that was
optimal once, might become less than optimal as tha data in tables change.
dean
"Ram Kumar Koditala" <RamKumarKoditala@.discussions.microsoft.com> wrote in
message news:CDC6F3B0-4377-4801-BDD2-A696BF76B791@.microsoft.com...
> Run your query by enabling Execution plan in Query Analyzer
> Thanks,
> RK
> "Akshay Sen" wrote:
>
as

Indexing Results of Stored Proc (or new table created by one)

Hi,

I am using data from multiple databases and/or queries. It would greatly
simplify and speed things up if I could use CONTAINS in processing the
results. However, "CONTAINS" requires the data to be indexed. Due to the
amount of processing, I think it would be faster even if I had to re-index
every time.

For example, I would like to do something like this (simplified to
illustrate the desired functionality... This should show all of the words
from one table that are not contained in their current or inflectional forms
within another table):

SELECT W1.Content
FROM
(SELECT Word AS Content
FROM MyTable) W1
LEFT OUTER JOIN
(SELECT Phrase AS Content
FROM MyOtherTable) W2
ON W2.Content CONTAINS(INFLECTIONAL, W1.Content)
WHERE W2.Content IS NULL

Can the results of a procedure be indexed? If not, can I drop the results
into a new table and trigger an automatic index of it, pausing the procedure
until the indexing is done?

Or, it there another way?

Thanks!"HumanJHawkins" <JHawkins@.HumanitiesSoftware.Com> wrote in message
news:sRhdc.1459$k05.510@.newsread2.news.pas.earthli nk.net...
> Hi,
> I am using data from multiple databases and/or queries. It would greatly
> simplify and speed things up if I could use CONTAINS in processing the
> results. However, "CONTAINS" requires the data to be indexed. Due to the
> amount of processing, I think it would be faster even if I had to re-index
> every time.
> For example, I would like to do something like this (simplified to
> illustrate the desired functionality... This should show all of the words
> from one table that are not contained in their current or inflectional
forms
> within another table):
> SELECT W1.Content
> FROM
> (SELECT Word AS Content
> FROM MyTable) W1
> LEFT OUTER JOIN
> (SELECT Phrase AS Content
> FROM MyOtherTable) W2
> ON W2.Content CONTAINS(INFLECTIONAL, W1.Content)
> WHERE W2.Content IS NULL
> Can the results of a procedure be indexed? If not, can I drop the results
> into a new table and trigger an automatic index of it, pausing the
procedure
> until the indexing is done?
> Or, it there another way?
> Thanks!

You may be able to use CONTAINSTABLE() instead, as it returns a table, which
you can then join on. But I'm not really familiar with it -
microsoft.public.sqlserver.fulltext may be a better place to ask.

As a general answer, you could start fulltext indexing on a table from
within a stored procedure, but that's likely to be very slow and there may
be security implications as well.

Simon|||"HumanJHawkins" <JHawkins@.HumanitiesSoftware.Com> wrote in message
news:sRhdc.1459$k05.510@.newsread2.news.pas.earthli nk.net...
> Hi,
> I am using data from multiple databases and/or queries. It would greatly
> simplify and speed things up if I could use CONTAINS in processing the
> results. However, "CONTAINS" requires the data to be indexed. Due to the
> amount of processing, I think it would be faster even if I had to re-index
> every time.
> For example, I would like to do something like this (simplified to
> illustrate the desired functionality... This should show all of the words
> from one table that are not contained in their current or inflectional
forms
> within another table):
> SELECT W1.Content
> FROM
> (SELECT Word AS Content
> FROM MyTable) W1
> LEFT OUTER JOIN
> (SELECT Phrase AS Content
> FROM MyOtherTable) W2
> ON W2.Content CONTAINS(INFLECTIONAL, W1.Content)
> WHERE W2.Content IS NULL
> Can the results of a procedure be indexed? If not, can I drop the results
> into a new table and trigger an automatic index of it, pausing the
procedure
> until the indexing is done?
> Or, it there another way?
> Thanks!

You may be able to use CONTAINSTABLE() instead, as it returns a table, which
you can then join on. But I'm not really familiar with it -
microsoft.public.sqlserver.fulltext may be a better place to ask.

As a general answer, you could start fulltext indexing on a table from
within a stored procedure, but that's likely to be very slow and there may
be security implications as well.

Simonsql

Monday, March 19, 2012

indexing distinct queries ...

Hi,
I have a table that contains log data, usually around a million records. The
table has about 10 columns with various attributes of the logged data,
nothing special. We're using SQL Server 2000.
Some of the columns (for example "category") have duplicate values
throughout the records. We have a web page that queries the table to show
all the unique columns, for example:
select distinct CATEGORY from table TEST
Obviously the server has to scan all rows in order to get all unique columns
which takes quite a while, especially since that web page contains several
of these types of queries. We also have a MAX(DATE) and MIN(DATE) query that
also add to the load.
I already created indexes on the CATEGORY (actually on all categories)
column which might help a little but I'm pretty sure that there has got to
be a better way.
I also create a view (select distinct CATEGORY from table TEST) and tried to
index it, but it won't let me index a query that contains a DISTINCT
statement.
Isn't there a way to create an index that contains only the distinct values?
Is there another way to speed this up?
Thanks for any hints!try creating a view with schema binding and use group by (which will return
the distinct values), then you should be able to add a unique index on the v
iew.|||Thanks, that actually seemed to work. I had tried this before but
discouraged by (yet another) error message mentioning count_big(*) missing.
Now I realized that I can just add the count_big(*) and it saved the index.
Initial Tests look promising - thanks.
"mark baekdal" <anonymous@.discussions.microsoft.com> wrote in message
news:BD3A168C-90FD-4FDC-AE1C-46618C6FF5A6@.microsoft.com...
> try creating a view with schema binding and use group by (which will
return the distinct values), then you should be able to add a unique index
on the view.

indexing distinct queries ...

Hi,
I have a table that contains log data, usually around a million records. The
table has about 10 columns with various attributes of the logged data,
nothing special. We're using SQL Server 2000.
Some of the columns (for example "category") have duplicate values
throughout the records. We have a web page that queries the table to show
all the unique columns, for example:
select distinct CATEGORY from table TEST
Obviously the server has to scan all rows in order to get all unique columns
which takes quite a while, especially since that web page contains several
of these types of queries. We also have a MAX(DATE) and MIN(DATE) query that
also add to the load.
I already created indexes on the CATEGORY (actually on all categories)
column which might help a little but I'm pretty sure that there has got to
be a better way.
I also create a view (select distinct CATEGORY from table TEST) and tried to
index it, but it won't let me index a query that contains a DISTINCT
statement.
Isn't there a way to create an index that contains only the distinct values?
Is there another way to speed this up?
Thanks for any hints!try creating a view with schema binding and use group by (which will return the distinct values), then you should be able to add a unique index on the view.|||Thanks, that actually seemed to work. I had tried this before but
discouraged by (yet another) error message mentioning count_big(*) missing.
Now I realized that I can just add the count_big(*) and it saved the index.
Initial Tests look promising - thanks.
"mark baekdal" <anonymous@.discussions.microsoft.com> wrote in message
news:BD3A168C-90FD-4FDC-AE1C-46618C6FF5A6@.microsoft.com...
> try creating a view with schema binding and use group by (which will
return the distinct values), then you should be able to add a unique index
on the view.

Monday, March 12, 2012

Indexing datetime field for selecting dates ranges

Hi,
I have a table with a smalldatetime field. Some of the queries in my
application are using range searches over that smalldatetime field, such as
selecting all the records within a date range. None of these queries are
selecting records from a specific date/time. They all work on ranges (e.g.
using BETWEEN or operators like >=).
Is there any reason for indexing the smalldatetime field? Could that make
the queries run faster?
Regards,
Amir.Yes, indexes on those columns can be beneficial, just as indexes on any colu
mn. You need to make
sure that your query is written in a way so that those indexes can be used
(http://www.karaszi.com/SQLServer/info_datetime.asp), of course. And whether
the indexes then *will*
be used is dependent on a lot of factors (the query, the data, selectivity e
tc).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Amir" <agamy@.actcom.co.il> wrote in message news:OCbLcq9FGHA.1032@.TK2MSFTNGP15.phx.gbl...[
color=darkred]
> Hi,
> I have a table with a smalldatetime field. Some of the queries in my appli
cation are using range
> searches over that smalldatetime field, such as selecting all the records
within a date range.
> None of these queries are selecting records from a specific date/time. The
y all work on ranges
> (e.g. using BETWEEN or operators like >=).
> Is there any reason for indexing the smalldatetime field? Could that make
the queries run faster?
> Regards,
> Amir.
>[/color]|||Thanks for the explanation!
Kind Regards,
Amir.
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:uOlH7mDGGHA.1424@.TK2MSFTNGP12.phx.gbl...
> Yes, indexes on those columns can be beneficial, just as indexes on any
> column. You need to make sure that your query is written in a way so that
> those indexes can be used
> (http://www.karaszi.com/SQLServer/info_datetime.asp), of course. And
> whether the indexes then *will* be used is dependent on a lot of factors
> (the query, the data, selectivity etc).
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Amir" <agamy@.actcom.co.il> wrote in message
> news:OCbLcq9FGHA.1032@.TK2MSFTNGP15.phx.gbl...
>

Friday, March 9, 2012

indexes.

Hi.
I have 2 queries that are similiar
They involve 2 tables,
1) CDR which has a primary key of cdrid and has half a million rows
2) MODULE which has a primary key of moduleid and has only 240000 rows
Neither of the two tables have indexes in it aside from the clustered index
produced by the primary key constraint
the two queries are as follows
Query #1) select cdr.cdrid from cdr, module where cdr.cdrid <> module.cdrid
Query #2) select cdr.cdrid from cdr, module where cdr.cdrid = module.cdrid
Query #1 takes over 40 minutes to run
Query #2 takes 8 seconds to run.
Currently, I am making an indexed view across the module and cdr tables
hoping that it will speed up the performance of query #2, but in the
meantime, I don't understand why Query #2 is so significantly different in
performance than query #1. Can anyone please explain this to me?
Thanks,
-- Jasonlooks like query #1 is almost a cross join:
a true cross join:
select cdr.cdrid from cdr, module
would return 500K*250K rows, that's a lot. the criteria cdr.cdrid <>
module.cdrid
does not filter out many riows, right?|||The queries take a long time because you are using a cross join, or cartesia
n
product of the two tables.
1. select cdr.cdrid from cdr, module
this produces a result set that is 500,000 x 240,000 = 120,000,000 rows,
which is then reduced by your WHERE clause. Instead, try
SELECT cdr.cdrid FROM cdr WHERE cdr.cdrid NOT IN (SELECT cdrid FROM module)
or
SELECT a.cdrid FROM cdr a
WHERE NOT EXISTS (SELECT b.cdrid FROM module b WHERE a.cdrid=b.cdrid)
Similarly, for #2, try
SELECT cdr.cdrid FROM cdr WHERE cdr.cdrid IN (SELECT cdrid FROM module)
"Jason" wrote:

> Hi.
> I have 2 queries that are similiar
> They involve 2 tables,
> 1) CDR which has a primary key of cdrid and has half a million rows
> 2) MODULE which has a primary key of moduleid and has only 240000 rows
> Neither of the two tables have indexes in it aside from the clustered inde
x
> produced by the primary key constraint
> the two queries are as follows
> Query #1) select cdr.cdrid from cdr, module where cdr.cdrid <> module.cdri
d
> Query #2) select cdr.cdrid from cdr, module where cdr.cdrid = module.cdrid
> Query #1 takes over 40 minutes to run
> Query #2 takes 8 seconds to run.
> Currently, I am making an indexed view across the module and cdr tables
> hoping that it will speed up the performance of query #2, but in the
> meantime, I don't understand why Query #2 is so significantly different i
n
> performance than query #1. Can anyone please explain this to me?
> Thanks,
> -- Jason
>
>|||Alex, you are correct.
What I am really going for is something more like this:
select cdrid from cdr where cdrid not in (select cdrid from module)
In order to put this into an indexed view, I need to get rid of the subquery
though.
In a case like this, I think I need to write it as a left outer join (i
think).
so it would be like this:
select cdr.cdrid from cdr
left outer join module on cdr.cdrid <> module.cdrid
I think that that is still incorrect though because it returns far too many
results. Can someone please point out my error?
Thanks,
-- Jason|||Since you only care about matching records shouldn't you be using an INNER
join?
select cdr.cdrid from cdr
inner join module on cdr.cdrid <> module.cdrid
John
"Jason" <jason@.acd.net> wrote in message
news:DrCdnSaeYOOKoDXe4p2dnA@.giganews.com...
> Alex, you are correct.
> What I am really going for is something more like this:
> select cdrid from cdr where cdrid not in (select cdrid from module)
> In order to put this into an indexed view, I need to get rid of the
> subquery though.
> In a case like this, I think I need to write it as a left outer join (i
> think).
> so it would be like this:
> select cdr.cdrid from cdr
> left outer join module on cdr.cdrid <> module.cdrid
> I think that that is still incorrect though because it returns far too
> many results. Can someone please point out my error?
> Thanks,
> -- Jason
>|||I think I figured it out...
On the contrary, I only care about not-matching records... In this case, I
matched up the CDR records to the MODULE records using a left outer join...
and then used the where clause for filtering out the ones that had actual
matches... as in the following:
select cdr.cdrid, module.cdrid from cdr
left outer join module on cdr.cdrid = module.cdrid
where module.cdrid is null
seems to work, but because it has an outer join, I cant put it into an
indexed view. *sigh*
"John Kendrick" <jkendrick@.DONTneo.SPAMrr.com> wrote in message
news:eQaorzYBGHA.3396@.tk2msftngp13.phx.gbl...
> Since you only care about matching records shouldn't you be using an INNER
> join?
> select cdr.cdrid from cdr
> inner join module on cdr.cdrid <> module.cdrid
> John
> "Jason" <jason@.acd.net> wrote in message
> news:DrCdnSaeYOOKoDXe4p2dnA@.giganews.com...
>|||On Tue, 20 Dec 2005 16:06:36 -0500, Jason wrote:

>I think I figured it out...
>On the contrary, I only care about not-matching records... In this case, I
>matched up the CDR records to the MODULE records using a left outer join...
>and then used the where clause for filtering out the ones that had actual
>matches... as in the following:
>select cdr.cdrid, module.cdrid from cdr
> left outer join module on cdr.cdrid = module.cdrid
>where module.cdrid is null
>seems to work, but because it has an outer join, I cant put it into an
>indexed view. *sigh*
Hi Jason,
Any specific reason why you want this in an indexed view?
From the preceeding post, I gather that this is only in an attempt to
increase performance. Why not try the easier options first? Such as
creating a nonclustered index on module.cdrid?
CREATE INDEX NameGoesHere ON module (cdrid)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

Wednesday, March 7, 2012

Indexes design

If I have a table with Col1 + Col2 as PK. But most of the queries use Col1, Col2, Col3 and Col4 in the queries, should I create another unique index on this combination? If not, my queries do an index scan. Just wondering is there a general rule for this kind of situation as I have a lot of such cases in my database.depends. most of the time multi column indexes are not a good idea. if i remember correctly for multi column indexes to work at all, the query has to search the columns in the table in the order that you defined the columns in the index.

index scans are ok, it is table scans you have to worry about.

are you experiencing a performance issue?|||Thrasymachus- I agree, we have to include all the columns in the index in the where clause to avoid a index scan. But the table is huge and yes, I am experiencing performance issues. I know I could make them run better with a seek.|||what's huge? tens of millions of records?

what's the ddl like? lots of fields? big fields?

And if you go ahead with this index, remember the order inwhich you search has to match the order of index definition.

What's the query look like?|||3 million records. Table has 9 columns with 2 varchar(512) and a UniqueIdentifier.

Not sure what you mean by "And if you go ahead with this index, remember the order inwhich you search has to match the order of index definition."|||3 million is not that big.

If your index is created like so

CREATE INDEX MyIndex On Table1 (col1,col2,col3)

Then your query should go like

SELECT *
FROM Table1
Where col1 = @.param1
AND col2 = @.param2
AND col3 = @.param3

same order, see.

ALSO I JUST REMEMBERED, the most selective column in the index definition should come first. That is col1 in the index definintion should have the highest number of distinct values.|||I am not sure if the order of the columns in the index and the query really matters. QO is smart enough to re-phrase your query to use the index. But if your query's where clause is on col1 and col3 while your index is on (col1, col2 and col3) then there will be a scan versus a seek.|||I develop in sql 2k now, but I just looked at my old design book for sql 7 I have laying around to make sure and this part I did confirm. things might have changed. The other thing I remeber from an ex-colleague of mine who was the sql guru I ever knew.

"ALSO I JUST REMEMBERED, the most selective column in the index definition should come first. That is col1 in the index definintion should have the highest number of distinct values."|||I am not sure if the order of the columns in the index and the query really matters. QO is smart enough to re-phrase your query to use the index. But if your query's where clause is on col1 and col3 while your index is on (col1, col2 and col3) then there will be a scan versus a seek.

The notion that the WHERE clause search arguments have to be in the "same order " as the columns defined in the key of the index is rubbish.

Also, the term "index scan" to me means examining EVERY row of an index because to find a match on a search criteria. In your example, "where clause is on col1 and col3 while your index is on (col1, col2, col3)", given that you don't have any datatype mismatch issues, and given that the index is chosen as the access path, and given that the search argument operator is "=", and given that the index is large enough, then you will KEY POSITION on col1, and scan the rest of the index for col3 matches. So, every row of the index containing col1 value is "scanned", but no more than that.|||KSherlock- I agree with you. I have the same understanding as yours. We got side tracked into this discussion about column order. My original question is a little different. Still looking for some comments/suggestions on it.|||As for this part:

"ALSO I JUST REMEMBERED, the most selective column in the index definition should come first. That is col1 in the index definintion should have the highest number of distinct values."

I will refer you to Database Design on SQL Server 7 by certification insider press page 173.

As for the other thing. This kind of backs me up (taken from http://www.sql-server-performance.com/composite_indexes.asp). I have forgot a little of it:

"A composite index is generally only useful to a query if the WHERE clause of the query matches the column(s) that are leftmost in the index. So if you create a composite index, such as "City, State", then a query such as "WHERE City = 'Springfield'" will use the index, but the query "WHERE STATE = 'MO'" will not use the index. [6.5, 7.0, 2000] Updated 11-15-2004"

So heres mud in your eye Sherlock.|||Check me if I have this straight:

Col1 and Col2 make up the PK

And most queries use all 4 Col1 Col2 Col3 and Col4? Or is that "or"? After all, if you specify col1 and col2, specifying col3 and col4 would be a bit redundant, eh?

If you specify values for Col1 and Col2, you should be seeking on the PK. Are you using functions on the columns? Like

where upper(col1) = upper(some search argument)

The above will cause a table scan every time. Since the PK is usually clustered, you may be seeing this just represented as a clustered index scan. It is really the same thing.|||And if you go ahead with this index, remember the order inwhich you search has to match the order of index definition

...

"A composite index is generally only useful to a query if the WHERE clause of the query matches the column(s) that are leftmost in the index. So if you create a composite index, such as "City, State", then a query such as "WHERE City = 'Springfield'" will use the index, but the query "WHERE STATE = 'MO'" will not use the index. [6.5, 7.0, 2000] Updated 11-15-2004"

So heres mud in your eye Sherlock.

Uhhh, yea. Whatever you say...|||...most of the time multi column indexes are not a good idea...Huh? Please present your theories as such, not as a "proven fact", man. You want the guy to loose his job just because you couldn't resist making a statement?

...ALSO I JUST REMEMBERED, the most selective column in the index definition should come first. That is col1 in the index definintion should have the highest number of distinct values...Good point, but ... how should I say it? It's orthodox concept for a table that is NEVER a part of an action query. Introduction of indexes based on "best practices for SELECT queries" ONLY, - is a recepie for your action queries as a potential bottleneck.

Indexes degrade too fast

(Win2003, SQL Server 2000 SP4)

I have a database of about 5Gb of size. Some queries where taking more than 1 minute to complete execution (all of them are stored procedures). Because of that lack of performance, I call the command DBREINDEX for each table, executed the sp_updatestats system stored procedure and finally I executed the sp_recompile system stored procedure for each sp in my database.

After all this task, queries completed in a matter of a few seconds instead of minutes. Strange enough is that some hours later (about 6 hrs), after normal use (this database belong to a Client/Server information system), the problem appeared again: Queries started to take too long to complete.

I am assuming that indexes are degrading too fast so that they required another ReIndex, but I am not sure.

Any thoughts? How can I prevent this behaviour?

Thank a lot in advanced.Have a look at the execution plan before and after the re-indexing routine. This will give you the "good" plan, and the "bad" plan. If the plans are identical, then the reindexing helped (doubtful but possible on a 5Gb system). More likely, the sp_recompile bumped the plan back from the "bad" plan to the "good" plan. By now, you are wondering if I just like putting "good" and "bad" in quotes. Well, no, I am not particularly fond of it, but it does remind you that not all plans are created equal. Can these procedures take in parameters that can determine a range? If so, then the "bad" plan may be good for some ranges, and the "good" plan may be bad for the same range.|||Thanks. What I really find confusing is why the performance of the database degrade in a matter of just 6 hours or so.

Of all the customers that I have distributed the same DB design, this one is the only one that presents this problem; and the size of the database is not the issue: I have other customer that has a 15 Gb database with the same DB design, then same DBMS version but with a not-as-good hardware. However, this customer does not have the same problem.|||What does DBCC SHOWCONTIG show you on the tables in question once the slow down restarts? Do you see high fragmentation numbers and low density numbers? Then yes your indices have degraded. have you traced the 2 systems. Does the one that is experiencing the issue have more insert and updates goinng on? Is it growing at a faster rate?|||Thanks. I will check that out. Just in case: Which DBCC command allow me to defrag all the tables of a database? I am planning to run that command and later run a DBCC REINDEX command for each index of every table. I suppose that this would produce a clean database, am I right?

Thanks in advance.|||There is no command to rebuild all of the tables/indexes at once. You can create or google for scripts that will do that for you, however.

indexes being ignored

sql2k sp3
Two boxes. Same db's, tables, indexes, queries. Indexes
are being used by 1 but not the other. Any ideas?
Thanks in advance.I'd check the statistics on the tables in question (SELECT
STATS_DATE(id, indid) FROM sysindexes). If the table that
isn't using an index has older statistics, then update
them.
I'm assuming the data content of the table is identical
(same number of rows, same result set, etc.).
Bruce.
>--Original Message--
>sql2k sp3
>Two boxes. Same db's, tables, indexes, queries. Indexes
>are being used by 1 but not the other. Any ideas?
>Thanks in advance.
>.
>|||I tried updating the stats to no avail. I didnt realize
amount of data would have any effect. The one ignoring the
indexes has data. The one using the indexes is empty. Any
ideas?
>--Original Message--
>I'd check the statistics on the tables in question
(SELECT
>STATS_DATE(id, indid) FROM sysindexes). If the table
that
>isn't using an index has older statistics, then update
>them.
>I'm assuming the data content of the table is identical
>(same number of rows, same result set, etc.).
>Bruce.
>>--Original Message--
>>sql2k sp3
>>Two boxes. Same db's, tables, indexes, queries. Indexes
>>are being used by 1 but not the other. Any ideas?
>>Thanks in advance.
>>.
>.
>|||If the selectivity of the value is too low it may not use the index. If you
don't have any data then the selectivity is pretty high<g>.
--
Andrew J. Kelly
SQL Server MVP
"chris" <chrisr@.fingps.com> wrote in message
news:050601c3789d$d6ec6100$a101280a@.phx.gbl...
> I tried updating the stats to no avail. I didnt realize
> amount of data would have any effect. The one ignoring the
> indexes has data. The one using the indexes is empty. Any
> ideas?
>
> >--Original Message--
> >I'd check the statistics on the tables in question
> (SELECT
> >STATS_DATE(id, indid) FROM sysindexes). If the table
> that
> >isn't using an index has older statistics, then update
> >them.
> >
> >I'm assuming the data content of the table is identical
> >(same number of rows, same result set, etc.).
> >
> >Bruce.
> >
> >>--Original Message--
> >>sql2k sp3
> >>
> >>Two boxes. Same db's, tables, indexes, queries. Indexes
> >>are being used by 1 but not the other. Any ideas?
> >>
> >>Thanks in advance.
> >>.
> >>
> >.
> >|||Are the physical machines the same? Same number of CPUs
and Memory? If not then execution plans can be different.
>--Original Message--
>If the selectivity of the value is too low it may not use
the index. If you
>don't have any data then the selectivity is pretty
high<g>.
>--
>Andrew J. Kelly
>SQL Server MVP
>
>"chris" <chrisr@.fingps.com> wrote in message
>news:050601c3789d$d6ec6100$a101280a@.phx.gbl...
>> I tried updating the stats to no avail. I didnt realize
>> amount of data would have any effect. The one ignoring
the
>> indexes has data. The one using the indexes is empty.
Any
>> ideas?
>>
>> >--Original Message--
>> >I'd check the statistics on the tables in question
>> (SELECT
>> >STATS_DATE(id, indid) FROM sysindexes). If the table
>> that
>> >isn't using an index has older statistics, then update
>> >them.
>> >
>> >I'm assuming the data content of the table is identical
>> >(same number of rows, same result set, etc.).
>> >
>> >Bruce.
>> >
>> >>--Original Message--
>> >>sql2k sp3
>> >>
>> >>Two boxes. Same db's, tables, indexes, queries.
Indexes
>> >>are being used by 1 but not the other. Any ideas?
>> >>
>> >>Thanks in advance.
>> >>.
>> >>
>> >.
>> >
>
>.
>

Friday, February 24, 2012

Indexes and Deadlocks

What are the exact reasons heavily fragmented Indexes can cause Deadlocks?
Is it purely because queries will run faster without fragmentation,
therefore lock escalation will happen quicker?
TIA, ChrisRbasically, yes.
Greg Jackson
PDX, Oregon

Indexes and Deadlocks

What are the exact reasons heavily fragmented Indexes can cause Deadlocks?
Is it purely because queries will run faster without fragmentation,
therefore lock escalation will happen quicker?
TIA, ChrisRbasically, yes.
Greg Jackson
PDX, Oregon

Indexes and Deadlocks

What are the exact reasons heavily fragmented Indexes can cause Deadlocks?
Is it purely because queries will run faster without fragmentation,
therefore lock escalation will happen quicker?
TIA, ChrisR
basically, yes.
Greg Jackson
PDX, Oregon

Indexes

I have a SQL Server 2000 database with numerous cluster and non-cluster
indexes.
I have a stored procedure that has several queries. This stored procedure
has query that uses index B3 first and then another query that uses B4 index
.
What is the best practice to have two separate indexes or one index (B3/B4)
that used by this stored procedure?
Thank You,
Table A
B1 : Cluster
B2
B3 :Non-Cluster
B4 :Non-ClusterThere is no best practice in that regard. Use whatever indexes improve your
performance, but try not to create too many indexes or you will end up
hurting data modification performance.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
news:B66C6D12-3E2E-4171-AABB-6FA620517275@.microsoft.com...
> I have a SQL Server 2000 database with numerous cluster and non-cluster
> indexes.
> I have a stored procedure that has several queries. This stored procedure
> has query that uses index B3 first and then another query that uses B4
> index.
> What is the best practice to have two separate indexes or one index
> (B3/B4)
> that used by this stored procedure?
> Thank You,
>
> Table A
> B1 : Cluster
> B2
> B3 :Non-Cluster
> B4 :Non-Cluster
>
>

Indexes

I have a SQL Server 2000 database with numerous cluster and non-cluster
indexes.
I have a stored procedure that has several queries. This stored procedure
has query that uses index B3 first and then another query that uses B4 index.
What is the best practice to have two separate indexes or one index (B3/B4)
that used by this stored procedure?
Thank You,
Table A
B1 : Cluster
B2
B3 :Non-Cluster
B4 :Non-ClusterThere is no best practice in that regard. Use whatever indexes improve your
performance, but try not to create too many indexes or you will end up
hurting data modification performance.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
--
"Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
news:B66C6D12-3E2E-4171-AABB-6FA620517275@.microsoft.com...
> I have a SQL Server 2000 database with numerous cluster and non-cluster
> indexes.
> I have a stored procedure that has several queries. This stored procedure
> has query that uses index B3 first and then another query that uses B4
> index.
> What is the best practice to have two separate indexes or one index
> (B3/B4)
> that used by this stored procedure?
> Thank You,
>
> Table A
> B1 : Cluster
> B2
> B3 :Non-Cluster
> B4 :Non-Cluster
>
>

Sunday, February 19, 2012

Indexes

I have a SQL Server 2000 database with numerous cluster and non-cluster
indexes.
I have a stored procedure that has several queries. This stored procedure
has query that uses index B3 first and then another query that uses B4 index.
What is the best practice to have two separate indexes or one index (B3/B4)
that used by this stored procedure?
Thank You,
Table A
B1 : Cluster
B2
B3 :Non-Cluster
B4 :Non-Cluster
There is no best practice in that regard. Use whatever indexes improve your
performance, but try not to create too many indexes or you will end up
hurting data modification performance.
Adam Machanic
Pro SQL Server 2005, available now
http://www.apress.com/book/bookDisplay.html?bID=457
"Joe K." <Joe K.@.discussions.microsoft.com> wrote in message
news:B66C6D12-3E2E-4171-AABB-6FA620517275@.microsoft.com...
> I have a SQL Server 2000 database with numerous cluster and non-cluster
> indexes.
> I have a stored procedure that has several queries. This stored procedure
> has query that uses index B3 first and then another query that uses B4
> index.
> What is the best practice to have two separate indexes or one index
> (B3/B4)
> that used by this stored procedure?
> Thank You,
>
> Table A
> B1 : Cluster
> B2
> B3 :Non-Cluster
> B4 :Non-Cluster
>
>