Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 optimization question

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-01-21 : 13:20:56
Greetings

Trying to wrap my head with optimization, fun fun

I have a clustered unique index with two int type fields


books table
book_id int PK
ISBN_id int FK


index is
book_id ASC,
ISBN_id ASC



There are over 17million+ rows
It is doing clustered index seek with Cost: 48%, the query is fast but I want to plan for scaling when there could be n-million rows. Is there any way to plan for that so that things do not slow down as more rows are added other than partitioning? Can I reduce that 48% cost?

Thanks!








If you don't have the passion to help people, you have no passion

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-21 : 13:48:05
Why is 48% a problem?

Can you post the query and the execution plan?

Have you run the query with these?:

SET STATISTICS IO ON
SET STATISTICS TIME ON

Please post the output of these when you run the query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-01-21 : 13:56:14
The cost has to go somewhere. The cost of all operators has to sum to 100%. Also note those costs are estimates.

Can you post the query, table and index definitions and execution plan please.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-01-21 : 14:13:22
ha!

SET STATISTICS IO ON
SET STATISTICS TIME ON
exposes what is going on under the hood! thanks!
I see who is the culprit! I think. tbl3 in red has some issues need to check indexes

Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0
Table 'tbl1'. Scan count 0, logical reads 181380, physical reads 0
Table 'tbl2'. Scan count 1, logical reads 3000, physical reads 0
Table 'tbl3'. Scan count 29917, logical reads 129528, physical reads 00.
Table 'tbl4'. Scan count 65, logical reads 12919, physical reads 0
Table 'tbl5'. Scan count 1, logical reads 3, physical reads 0
Table 'tbl6'. Scan count 0, logical reads 3, physical reads 0
Table 'tbl7'. Scan count 1, logical reads 9466, physical reads 0reads 0.
Table 'tbl8'. Scan count 1, logical reads 210, physical reads 0


If you don't have the passion to help people, you have no passion
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-01-21 : 16:02:35
Ignore the scan count. It DOES NOT mean that the table was scanned. Focus on the logical reads

--
Gail Shaw
SQL Server MVP
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-01-21 : 16:12:10
tbl1 and tbl3 should be focused on as a starting port. tbl4 and tbl7 should be focused on after you get tbl1 and tbl3 logical reads down.

Have you run the missing indexes DMV report to see what SQL is recommending be added for these tables? How about if you run the query in Management Studio 2008 and view the execution plan (it'll indicate if there's a missing index)?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-01-21 : 16:36:10
Thank you all

working on DMV report and checking out what SQL recommends via execution plan. What should I specifically be looking out for in the ep?

If you don't have the passion to help people, you have no passion
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-01-22 : 03:25:49
It may suggest a missing index. If you're using SQL 2008's management studio it will be obvious. Test it, don't assume SQL's right.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2011-01-22 : 11:03:16
129528 scan indicates you are retrieving a hell lot of rows from the table.What is the average row size of the table?

PBUH

Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-02-02 : 11:44:05
does the fact that my high logical read tables are in another database than my main app database make a difference?

my query returns 666176 rows in 1 minute and 26 seconds in SSMS. On the UI end it times out. I have added the recommended indexes and still real slow. I am dealing with 25million+ rows on the core table

If you don't have the passion to help people, you have no passion
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-02 : 12:39:15
Why are you returning 666,176 rows? It will never be fast with that many rows.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-02-02 : 13:17:59
>> I have a clustered unique index with two INTEGER type fields [sic] <<

This looks really awful to me -- used to own some bookstores. Let's fix that skeleton table.

Columns are not fields, let's get the terms right. There is also no such thing as an "isbn_code" -- it is just the "isbn" and it is not an integer. And since it is an industry standard, it ought to the the PRIMARY KEY. What the heck is a "book_id" anyway?

CREATE TABLE Books
(isbn CHAR(13) NOT NULL PRIMARY KEY
CHECK (isbn LIKE '978[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
book_id INTEGER NOT NULL,
etc);

You say you have:

CREATE INDEX foobar ON Books (book_id ASC, isbn ASC);

Knowing the industry, I would drop the "book_id" and correct the ISBN's data type. Then scrub the data to be sure the check digit is right (which is probably is not with INTEGER data). Then you can use substrings to get to languages and publishers encoded in the ISBN.

That means the PRIMARY KEY has already given you an index.

I hope this was not your real problem. Can you post the actual problem?


--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -