.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Check Index Fragmentation query

Posted By:      Posted Date: September 10, 2010    Points: 0   Category :Sql Server
Hi, I have ran the following query to view the fragmentation on my large table with 1000+ pages. Now the results I got are moslty at index level 0 and 1 and most objects are fragmented 90+%. My question to the experts is that the query is displaying fragmentation mostly at Index level 0 and 1. Do I need to look or just Level = 0 is fine? The query is SELECT DB_NAME(SDDIPS.[database_id]) AS [database_name], OBJECT_NAME(SDDIPS.[object_id], DB_ID()) AS [object_name], sddips.[object_id] as [Object_ID] ,SSI.[name] AS [index_name], SDDIPS.partition_number, SDDIPS.index_level ,SDDIPS.index_type_desc, SDDIPS.alloc_unit_type_desc, SDDIPS.avg_fragmentation_in_percent], SDDIPS.[page_count] FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'detailed') SDDIPS INNER JOIN sys.sysindexes SSI ON SDDIPS.OBJECT_ID = SSI.id AND SDDIPS.index_id = SSI.indid WHERE SDDIPS.page_count > 1000 AND avg_fragmentation_in_percent > 15 AND index_type_desc <> 'HEAP' ORDER BY OBJECT_NAME(SDDIPS.[object_id], DB_ID()), index_id

View Complete Post

More Related Resource Links

SQL Query index

Hi, I am working on WM 6.0 MC75 device.  I am creating database using SQL compact server 3.0.  I have almost 30K records in device. When I execute simple query with Order BY it takes almost a minute to fetch data. Then I used index for where clause. It becomes fast and the time reduced to 7-8 sec.  But it creates problem in Insert/Update. As this takes ____ of time in inserting data to table. I dont know how to proceed further. As i stucked here. Please give ma a direction   Regards  Shruti

Check if Linq query return result

HelloI have Linq query likeDim Prood= (From p InProdutcts _                                          where p.ProductID="InputInteger"                                                         ..... ) .SingleOrDefaultI could Use Count if I Use .ToList Instead of .SingleOrDefault , But now that I use 'SingleOrDefault', how Could I find if Prod contains any record or it's Empty.

Query DB and update Check Box List on JS/jQuery Function

Hey guys.  I'm not sure if this is the right forum to post this in but I've come to a roadblock in my app and I'm not sure which way I should go.  Currently, I have a slider control and two text boxes used to set the Start and End times of a section of a video.  Let's say the video is of some Music Awards.  In my database, I've tagged the start and end time of each celebrities on-screen appearance.  When the user adjusts their start and end times on the page, I want to query the database table and pull back all celebrities within that range.  The problem is I don't know how to do this in JS.Can I use the Check Box List and bind the values to it?  Do I need to render a table of Check Boxes in JavaScript?  Do I call the Server-Side code via Ajax and then partially render the page?Any help, comments or suggestions would be great.

How index will effect in update query

Hi All, I have one update query it has lot of joins with other table ,If we add index in the secondary table with this join condition will it improve the performance? eg: UPDATE FunctionalAcknHeaders  SET cStatus = 'E'  FROM FunctionalAcknHeaders fh   LEFT JOIN OrderHeaders oh ON cast(oh.nOrderHeaders_id as varchar(10)) = 'abc'  WHERE oh.nOrderHeaders_id is null   AND fh.cStatus = 'R'   if i create index on OrderHeaders  table nOrderHeaders_id, will it improve the performance ? Thanks, Roshan

Query doesnt use right Index

Hi experts, This is really strange issue.  I have created a table on my dev environment first. Copied the data from another table. Created Indices / Pk and then used a query which doesnt want to use right index. I have NC Index on my Lastupdate , Log , type colmns and I have CI on my Identity col called "CluIndex". And when I use a query "select * from table where lastupdate >= '09/16/2010'", it takes for ever to get the result back. And when I check the Display query exec plan, it shows its using CI which is not right. If I use query hint with the right index, the results are quick. So basically query plan is not using the right index. Weird thing is that, this same table (I created myself with same steps) uses the right Index in production. Very strange!!!

SQL Query for Date Check User Dates



I am using two columns in one table such as StartDate,Enddate.

In asp.net Form two text boxes there. One is StartDate other one EndDate..

When user Click Submit button the Two dates is compare above DataFiled's...


-------- Data Base Values ----------------

StartDate            EndDate


10/09/2010         14/09/2010

16/09/2010         28/09/2010



user send Start Date ' 27/09/2010' and EndDate '30/09/2010'


Now the Query Check Dates Between Userdates avialable in Database....


What I ll do?????


(Note : Opertion only allow the Dates Not Exists in Database Dates)



Query defaults to a Clustered Index Scan instead of using an Index Seek


I have query that behaves much differently the moment I modify the Where clause


The following query runs fast!!





is there a query for index data in RAM?

Essentially I'm looking for a query that will give me the size of each table and index and the percentage of that object that is in RAM presently. I've got several queries to report the total size of the tables and indexes. I can't see how to determine if any of those data pages are presently in RAM. Is that information available with a performance query of some kind?

The query is using a index scan not seek.... how can I get it to use a seek.


The query is using a index scan not seek.... how can I get it to use a seek.

I am using SQL 2005

Mr Shaw

script to check fragmentation


Dear all,

Can anyone provide me the complete script to check the average fragmentation for each table (with table name) in a DB?

I have tried this but seem only retrive data for one of the table. I would like to show ALL tables.

SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID(N'xxxx.master'),
  JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id;








Index fragmentation script runs but yet fragmentation remains high


I have an index maintenance job that runs frequently, what I find is that despite the fact that the script runs, fragmentation is still quite high on the tables. I am using the standard approach of having  a rebuild if fragmentation is over 20 and if its between 5 and 20 then it reorganises the page.

Is this usual for such a behavior ?

How to tell if an index exists using a SQL query on SQL CE

On an SQL CE based mobile app, I need to be able to tell if an index has been created using a TSQL query.  Can some one point me in the right direction on this?

How to tell if an index exists using a SQL query on SQL CE

On an SQL CE based mobile app, I need to be able to tell if an index has been created using a TSQL query.  Can some one point me in the right direction on this?

Primary, Clustered, IDENTITY key index fragmentation


We have a table with an IDENTITY primary key and a clustered index on this key.  The SQL to create the table is shown below.  After inserting 100 records or less (records aren't deleted or updated, typically), the index is highly fragmented.  This eventually becomes a crucial bottleneck because the table is joined to another table with millions of records to produce a view.

It's easy enough to rebuild the index, but it would be optimal to prevent the fragmentation in the first place.


1) What causes the fragmentation? Is it the fact that the table contains a nvarchar(max) field?  Is it the clustered index?

2) Are there ways to prevent fragmentation in the first place, and if so, how?  I have tried setting PAD_INDEX to ON, and using FILLFACTORS of 30, 50, 80, 90, and 100, all to no avail.  I have also tried converting the nvarchar(max) to a nchar(400).

Thank you.

SQL Server Version: 2008

SQL to create the table, index:

CREATE TABLE [dbo].[MyStrings](
    [StringId] [int] IDENTITY(-2147483647,1) NOT NULL,
    [SecondaryKey] [binary](20) NOT NULL,
    [Text] [nvarchar](max) NOT NULL,

CAML query to check if user is member of a specific group


I've got a nested query caml which basically should check 3 things:

  1. Check if a task has been assigned directly to the ucrrent user
  2. Check if a task has been assigned to a group the user is in
  3. Check if the user is member of a specific

The first to bullet points work like a charm, but I can't figure out how to get the last one to work. Here is what I thought should work:

query += "<Where>";
query += "<Eq><Membership Type=\"CurrentUserGroups\">";
query += "<Value Type=\"String\">Name of my specific group</Value>";
query += "</Membership></Eq>";
query += "</Where>";

Somehow it doesn't seem to be the correct approach, is there somebody who can help me out on this? Thanks

Select query to check existence of foreign key



I have table A & B (One to Many). There is a foreign key in Table B. I have to write a quay to select data from A.

Its one Colum should be a Boolean value…. I.e. it should be True if related value (foreign key) exist in the table B. Other wise it false.

Please advice me efficient way of writing this query?

Using a CompareValidator to check input is a valid date

The CompareValidator can do more than just compare two controls. You can also compare it against several of the main .net data types such as Date, Integer, Double and Currency.

To do this you would set Operator="DataTypeCheck" and instead of setting the ControlToCompare or ValueToCompare attributes as you normally would you use the Type="Date" (or any of the data types I have listed above).
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend