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)
 high logical reads, high pages

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-20 : 11:59:09
Greetings

I am getting some very high logical reads and pages on two of my tables.

Table 'tblCustomers'. Scan count 0, logical reads 149017, physical reads 2, read-ahead reads 16564.
Table 'tblOrders'. Scan count 0, logical reads 149031, physical reads 0, read-ahead reads 1423.


When I do DBCC checktable on these two tables I get the following

tblCustomers 4610000 rows in 217710 pages
tblOrders 1982428 rows in 90611 pages


How do I go about resolving this issues so I reduce the logical reads and pages? I have clustered indexes on each tables' primary key.

Thanks

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

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-12-20 : 12:51:32
High reads typically indicate you are missing an index or you are returning too much data. You'll need to provide us more detail.

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 - 2010-12-20 : 13:20:00
I have all the possible indexes (per http://www.sqlskills.com/BLOGS/KIMBERLY/post/Disk-space-is-cheap.aspx) I have over 17million rows on both tables.

Clustered on PK
Non-clustered on other fields

Looks like I am bringing back too many rows. But why is there such high 217710 pages. How can I deallocate some of those pages? How can I find out which are empty?

Thanks

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

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-12-20 : 13:31:03
How log does it take to excute? there are onlye 2 physical reads going on, so that probably means almost all the data is in memory.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-20 : 13:41:04
OK i found one of the problems, it is the front end code that is chugging. running the query in SSMS takes 2-6 seconds execute :( pretty fast I would say

This is another issue. this database was first created in 2005 and then backed up and then imported into SQL 2008.
It is now in simple recovery mode, compatibility 2008 (100)



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

robvolk
Most Valuable Yak

15732 Posts

Posted - 2010-12-20 : 13:55:13
quote:
This is another issue. this database was first created in 2005 and then backed up and then imported into SQL 2008.
Make sure to run UPDATE STATISTICS on all tables if you haven't already done it.
Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-20 : 14:05:49
thanks robvolk. I ran that command on all the whole database. statistics did changed


Table 'tblCustomers'. Scan count 0, logical reads 91905, physical reads 0, read-ahead reads 0.
Table 'tblOrders'. Scan count 0, logical reads 91905, physical reads 0, read-ahead reads 0.


This is fascinating stuff! Never knew there was so much statistics !


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 - 2010-12-20 : 14:40:50
How many rows is the query returning?

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 - 2010-12-20 : 15:25:18
the query is returning 12211 rows

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 - 2010-12-20 : 15:27:29
There you go. Reads are going to be high for 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

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-20 : 15:37:30
thanks tara!

But what are there so many pages is that ? Is that normal?


tblCustomers 4610000 rows in 217710 pages
tblOrders 1982428 rows in 90611 pages

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 - 2010-12-20 : 15:51:27
We'd have to see your table structure, indexes, and output of the fragmentation DMV

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

Subscribe to my blog
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-12-21 : 04:29:32
Also it depends on the size of the row.You need to understand on how your rows are saved in the datapages.Have a look at the simple example below.

I created a single table with single column id.By default 2 datapages are assigned to it.
if not exists(select * from sys.databases where name='TestRowSize')

Create database TestRowSize
Go

if not exists(select * from sys.tables where name='t')
create table t (id int)

Use TestRowSize
Go

insert t
select 1

exec sp_spaceused t
go
insert into t
select 2
go 591

exec sp_spaceused t

dbcc ind('TestRowSize',t,-1)

insert into t
select 3

exec sp_spaceused t

dbcc ind('TestRowSize',t,-1)



drop table t


The o/p of the first exec sp_spaceused t is belows.



name rows reserved data index_size unused
t 1 16 KB 8 KB 8 KB 0 KB


So in the above example the total number of rows that can be saved on these two datapages are 592.So when I insert another row with value 3 another datapage is assigned to it.

So now when sp_spaceused t is executed the reserved column of the sp_spaceused t will return 24KB.You can see the PageID and the pagefileid which gives the exact location of the address for the pages in the datafiles.

The whole scenario will change when the datatype of the column id changes from int to tinyint as tinyint takes less data storage.So even for no of rows greater than 592 inserted in table t when datatype of id is tinyint only 2 datapages are used.

PBUH

Go to Top of Page

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2010-12-21 : 12:19:14
with that explanation I think I now know which fields are the culprits. We ingest data from external source with some fields set to varchar(max) to accomodate / delimited list. I will break these off into another table and just point to them via FK/PK. I have two of those fields with varcchar(max), that must bloat the pages!

thank you very much all

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 - 2010-12-21 : 12:30:06
Yes indeed varchar(max) bloats them! And moving them to a different table is a fantastic table. It's called vertical partitioning and can greatly help you in regards to performance.

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 - 2010-12-21 : 12:43:13
thank you Tara, thanks to you we all learn DMV , vertical partitioning etc. you are the best!

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 - 2010-12-21 : 13:06:13
You're welcome, glad to help.

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

Subscribe to my blog
Go to Top of Page
   

- Advertisement -