I had an interesting thing to find out this week. An Organization had an issue: Their queries are running extremely slow. They did some research on this. They found that even simple queries which uses index seek too are extremely slow. They copied the data into another database and created the indexes and executed again. They were quite fast. There were no other processes running when this test was carried out.
Through another friend of mine they got my contacts and they called me. I got the execution plans from both databases and they were identical. Then with the help from them, I ran a query using sys.dm_db_index_physical_stats against this index and found that it is fragmented. They rebuilt the index and the query started working well again.
Using the same DMV against other tables, I found out that the indexes are fragmented. They sent me the figures they received using dm_db_index_physical_stats Fragmented means some are 80% - 95% fragmented. To be honest with you, I even saw a few indexes showing 100% fragmentation which I have never seen before. I asked them to rebuild the indexes. Later I explained them how these house keeping jobs could be automated.
After a couple of weeks, they called me again. The indexes are fragmented again. I asked them whether they have added the index maintenance job. They answered back saying they have an index maintenance task as part of the database maintenance job and even made it as the first task of the job, [which runs on Sunday nights] but that has not resolved the issue.
All about - databases and related technologies - things I work with - SQL Server, other relational & non relational databases, PowerShell and scripting languages.
Nov 9, 2010
Nov 2, 2010
Red gate labs
Red gate has opened its lab for public viewing!
It is web site, where the tools developed are available for download. They include
Log Shipping Monitor, SQL Backup to MTF Converter (which converts a red-gate backup to native SQL Server backup and Red Gate Snapper (that allows you to create SQL Compare snapshot files for SQL Server databases).
The lab does include tools for .Net developers. Red Gate Memory Tracker (analyzing application memory usage, including loaded libraries, system heap memory and .NET memory) and Diagnostic Tool (used to collect useful system information) will help serious developers.
These are tiny tools, which i believe Red-gate may include or have included with their products. Now these tools come as free downloads.
I haven't tested all the applications yet, (in fact I haven't tested any of them) but I feel that most of them are really useful.
Try this link
http://labs.red-gate.com/
It is web site, where the tools developed are available for download. They include
Log Shipping Monitor, SQL Backup to MTF Converter (which converts a red-gate backup to native SQL Server backup and Red Gate Snapper (that allows you to create SQL Compare snapshot files for SQL Server databases).
The lab does include tools for .Net developers. Red Gate Memory Tracker (analyzing application memory usage, including loaded libraries, system heap memory and .NET memory) and Diagnostic Tool (used to collect useful system information) will help serious developers.
These are tiny tools, which i believe Red-gate may include or have included with their products. Now these tools come as free downloads.
I haven't tested all the applications yet, (in fact I haven't tested any of them) but I feel that most of them are really useful.
Try this link
http://labs.red-gate.com/
Oct 22, 2010
NO Recompilations when (parameter) values are different
Please read another post on this subject by Dinesh Priyankara.
http://dinesql.blogspot.com/2010/10/do-we-need-to-recompile-stored.html
This is another topic came in the last user group meeting:
Does SQL Server generate a new plan (in other word recompile) the query if the parameter value is changed?
The actual answer is it depends:
First of all, SQL Server does not go through the recompilation process simply because the parameter is changed.
As I mentioned in the previous blog, there are only certain conditions where an existing execution plan is thrown away and a new plan is used.
Even though in my example, a new plan is created between these two statements, the value is not the cause.
I removed the GO statement in between and tried too. The result was same. (except the column line number showed 1 and 2 instead of 1 always.
The image is attached.
Fortunately, SQL Server provides a few DMVs (dynamic management views) which allows us to check the execution plans
http://dinesql.blogspot.com/2010/10/do-we-need-to-recompile-stored.html
This is another topic came in the last user group meeting:
Does SQL Server generate a new plan (in other word recompile) the query if the parameter value is changed?
The actual answer is it depends:
First of all, SQL Server does not go through the recompilation process simply because the parameter is changed.
As I mentioned in the previous blog, there are only certain conditions where an existing execution plan is thrown away and a new plan is used.
Even though in my example, a new plan is created between these two statements, the value is not the cause.
DBCC FREEPROCCACHE
GO
SELECT * FROM Sales.SalesOrderDetail
WHERE SalesOrderDetailID <= 1
GO
SELECT * FROM Sales.SalesOrderDetail
WHERE SalesOrderDetailID <= 1000
GO
When I ran the queries and captures the SQL:Recompile event, nothing was captured. Interestingly, ShowPlan XML for Query Compilation too, didn't give any number as the reason for recompilation. For me it looked like SQL Server didn't consider them as same query. I removed the GO statement in between and tried too. The result was same. (except the column line number showed 1 and 2 instead of 1 always.
The image is attached.
Fortunately, SQL Server provides a few DMVs (dynamic management views) which allows us to check the execution plans
Oct 21, 2010
Recompilations after index creation
Yesterday I got an interesting question: Does adding an index to a table force the execution plans to be recreated?
Books online says this:
Please note the first, second and fourth bulleted points.
It says either a structure should be changed or index used by the query should be changed. In other words not any change in the index will not force recompile.
Books online says this:
It says either a structure should be changed or index used by the query should be changed. In other words not any change in the index will not force recompile.
Oct 20, 2010
Today I will be speaking at the user group meeting
As some of you know, SS SLUG (SQL Server Sri Lanka User Group) meets today. Generally we have two topics to discuss.
According to the plan we had earlier, I will be in the panel of the second topic (database design)
But due to certain unforeseen issues, at the last minute, I accepted the challenge to speak at today's user group meeting. That means I will speak on execution plans first and then will be in the panel for the second topic too.
If everything goes well with the plan, In today's presentation, I will talk about reading execution plan, various table and index operations, and the logic behind it. At the end of the session, I expect the users to guess the execution plan for simple queries
See you there!
According to the plan we had earlier, I will be in the panel of the second topic (database design)
But due to certain unforeseen issues, at the last minute, I accepted the challenge to speak at today's user group meeting. That means I will speak on execution plans first and then will be in the panel for the second topic too.
If everything goes well with the plan, In today's presentation, I will talk about reading execution plan, various table and index operations, and the logic behind it. At the end of the session, I expect the users to guess the execution plan for simple queries
See you there!
Jul 28, 2010
Trigger Happy at SSSLUG
Hope you were there at the last user group meeting!
I was talking about triggers. This was a beginner level session.
The code samples and the slide deck are uploaded at SSLUG. you ncan download them from here:
http://sqlserveruniverse.com/v2/SSSLUG/Downloads.aspx
I was talking about triggers. This was a beginner level session.
The code samples and the slide deck are uploaded at SSLUG. you ncan download them from here:
http://sqlserveruniverse.com/v2/SSSLUG/Downloads.aspx
Jun 20, 2010
Understanding REVOKE
This is one of the topics I was planning to write for a long time, but failed due to various reasons (and without valid reasons) So, this may sound too basic for some people; if so, please forgive me. I have seen much confusion over REVOKE statement. Some believe that revoke is a different word for DENY. As explaining the security model is worth a series of articles, in this post, I am going to explain only about REVOKE statement. For this, I am using AdventureWorks database. I created two logins which I am using throughout the post
Additionally I am adding these users to AdventureWorks. Two database roles also created for this purpose.
CREATE LOGIN [Sam] WITH PASSWORD=N'MyPassw0rd!_', DEFAULT_DATABASE=[AdventureWorks], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
CREATE LOGIN [Joe] WITH PASSWORD=N'MyPassw0rd!_', DEFAULT_DATABASE=[AdventureWorks], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
USE [AdventureWorks]
CREATE USER [Joe] FOR LOGIN [Joe] WITH DEFAULT_SCHEMA=[dbo]
CREATE USER [Sam] FOR LOGIN [Sam] WITH DEFAULT_SCHEMA=[dbo]
CREATE ROLE [Sales] AUTHORIZATION [dbo]
EXEC sp_addrolemember N'Sales', N'Joe'
EXEC sp_addrolemember N'Sales', N'Sam'
CREATE ROLE [Marketing] AUTHORIZATION [dbo]
EXEC sp_addrolemember N'Marketing', N'Sam'
Jun 18, 2010
Susantha's Presentation on Powershell
As you may know, Susantha Bathige, a fellow DBA at eCollege, gave a great presentation on Windows Powershell 2.0 at the last SQL Server User Group meeting. He had a few hick-ups when he was starting as the laptop couldn't communicate well with the projector. Gogula shared his laptop, but it refused to listen to his commands properly.
On content, presentation style and confidence level Susantha made an impression. I recommend it to all DBAs, developers and IT pro members.
For those who couldn't participate can view the sample code and presentation at here. The pictures taken were available at here (If you are wondering what I was doing when he was presenting, I was working as his assistant during his presentation. I hope it would have helped Susantha to concentrate on the presentation while I was controlling Gogula's laptop.)
Congratulations Susantha. It is very difficult to say that it was your first presentation. Keep it up.
On content, presentation style and confidence level Susantha made an impression. I recommend it to all DBAs, developers and IT pro members.
For those who couldn't participate can view the sample code and presentation at here. The pictures taken were available at here (If you are wondering what I was doing when he was presenting, I was working as his assistant during his presentation. I hope it would have helped Susantha to concentrate on the presentation while I was controlling Gogula's laptop.)
Congratulations Susantha. It is very difficult to say that it was your first presentation. Keep it up.
Jun 13, 2010
"She" is coming. .. I am excited. Are you?
For a change I am writing some thing different this time. This is not a technical article. As you can below, I have no tags to it.
What is her name? I don't know yet. But I know she is coming and I am excited about it.
If you are wondering who she is, I can tell you this: Even though I haven't seen her, I am sure she will be in her best, sexiest appearance when she appears. You are going to wait anxiously to see her coming and working in your office. You may want to work closely with her, see her internal organs and have complete control over her. Initially your office management will be scared / hesitated to take her in, but there is a high possibility she will make her way in. When she comes she will come with much “hosanna” from some, while some others will grumble about her. Those who grumble will find fault with the time she has taken to respond to the calls they have made, the way she is working and the time she takes to deliver results, and even how her internal organs are made. Interestingly only a few will grumble about her sexy appearance as everyone knows that there is no point in talking about it.
Even if she failed to join your firm, you will try to have some quality time with her. You may use your office and after office hours to gather information about her, listen from people who know her already and even make appointments to work with her. Sometimes you will work with her but mostly you will sit and watch when she is doing the hard work for you. Even though she may work very closely with you, she will remain a mystery for some time. Gradually you will start learning some secrets about her. You may be looking for a day to say at least this: “I know many things including some secrets about her”.
I am not sure about you, but I bet many like to have illegal relationship with her, as starting a legal relationship costs a lot these days, you know. Even though her parents will try to avoid these illegal relationships and she too will resist illegal relationships some may know how to get hold of her.
Initially no one will know her actual name, and the actual date she is going to join your firm. People will keep on guessing. Many will refer her with her pet name until they are used to the actual name.
Excited? Now I share the first secret I know about her: Her pet name is Denali
I need to caution you on this: As it happen often in other cases, when time goes, you may lose the excitement and even start looking for a new one.
Now it is time to share the next secret: Denali is the code name for the next version of SQL Server, Microsoft’s flagship database management system. Most probably you will see her coming to your place by next year. So better be ready soon.
What is her name? I don't know yet. But I know she is coming and I am excited about it.
If you are wondering who she is, I can tell you this: Even though I haven't seen her, I am sure she will be in her best, sexiest appearance when she appears. You are going to wait anxiously to see her coming and working in your office. You may want to work closely with her, see her internal organs and have complete control over her. Initially your office management will be scared / hesitated to take her in, but there is a high possibility she will make her way in. When she comes she will come with much “hosanna” from some, while some others will grumble about her. Those who grumble will find fault with the time she has taken to respond to the calls they have made, the way she is working and the time she takes to deliver results, and even how her internal organs are made. Interestingly only a few will grumble about her sexy appearance as everyone knows that there is no point in talking about it.
Even if she failed to join your firm, you will try to have some quality time with her. You may use your office and after office hours to gather information about her, listen from people who know her already and even make appointments to work with her. Sometimes you will work with her but mostly you will sit and watch when she is doing the hard work for you. Even though she may work very closely with you, she will remain a mystery for some time. Gradually you will start learning some secrets about her. You may be looking for a day to say at least this: “I know many things including some secrets about her”.
I am not sure about you, but I bet many like to have illegal relationship with her, as starting a legal relationship costs a lot these days, you know. Even though her parents will try to avoid these illegal relationships and she too will resist illegal relationships some may know how to get hold of her.
Initially no one will know her actual name, and the actual date she is going to join your firm. People will keep on guessing. Many will refer her with her pet name until they are used to the actual name.
Excited? Now I share the first secret I know about her: Her pet name is Denali
I need to caution you on this: As it happen often in other cases, when time goes, you may lose the excitement and even start looking for a new one.
Now it is time to share the next secret: Denali is the code name for the next version of SQL Server, Microsoft’s flagship database management system. Most probably you will see her coming to your place by next year. So better be ready soon.
Jun 12, 2010
Constraints on Temp tables
Yesterday, I had an interesting conversation on temp tables:
All started with this question: What is the best method to create constraints on temp tables? I never expected it to give me a chance to learn something new.
As you know Temp tables are created in tempdb. Local temp tables are visible only to the user, and it allows multiple users to have temp tables on their own. That means while user A has a temp table #MyFirstTable another user can have a table (at the same time) at the same name.
Generally all objects created in a database will have an entry in sys.objects. For the table, the entry will not be on the same name. SQL Server will add additional characters to create a unique name. The objects are dropped when the connection is dropped or reset.
But there is a difference when it comes to constraints. When a constraint is created, it created with the exact name specified and it will have an entry in sys.objects table. For example the following code is going to fail if multiple users are going to execute during the same period.
The reason for failure is nothing to do with syntax. When the first user executes it two entries into sys.objects: a row with system defined name for table and another row for name we specified for primary key. When the second user executes the code, it can add the row for table but it can’t add an entry for primary key. So it will fail for second user.
All started with this question: What is the best method to create constraints on temp tables? I never expected it to give me a chance to learn something new.
As you know Temp tables are created in tempdb. Local temp tables are visible only to the user, and it allows multiple users to have temp tables on their own. That means while user A has a temp table #MyFirstTable another user can have a table (at the same time) at the same name.
Generally all objects created in a database will have an entry in sys.objects. For the table, the entry will not be on the same name. SQL Server will add additional characters to create a unique name. The objects are dropped when the connection is dropped or reset.
But there is a difference when it comes to constraints. When a constraint is created, it created with the exact name specified and it will have an entry in sys.objects table. For example the following code is going to fail if multiple users are going to execute during the same period.
CREATE TABLE #MyFirstTable
(
MyFirstTable_PK int,
CONSTRAINT PK_#MyFirstTable PRIMARY KEY CLUSTERED (MyFirstTable_PK)
)
The reason for failure is nothing to do with syntax. When the first user executes it two entries into sys.objects: a row with system defined name for table and another row for name we specified for primary key. When the second user executes the code, it can add the row for table but it can’t add an entry for primary key. So it will fail for second user.
Subscribe to:
Posts (Atom)
