Sunday, March 11, 2012
Compare Queries
to see which one is the most efficient. I am trying to use the execution
plan but it doesn't give an overall cost of the query so I can compare it to
the alternative query. Rather I get a break down of the various steps (this
query uses functions which produce aggregates and these steps are included
in the execution plan) . I want to explore the steps later, at this point I
just want to compare the two queries as a whole. What is the best way of
going about this? Regards, Chris.What I'd do to start is to fire up SQL Server Profiler, turn on the
SQL:BatchCompleted event, and collect the Reads, Writes, CPU, and Duration
columns. Run your queries and compare the output...
Adam Machanic
SQL Server MVP
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
"Chris" <nospam@.nospam.com> wrote in message
news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
>I am creating an SP. I want to compare alternative ways of writing the
>query to see which one is the most efficient. I am trying to use the
>execution plan but it doesn't give an overall cost of the query so I can
>compare it to the alternative query. Rather I get a break down of the
>various steps (this query uses functions which produce aggregates and these
>steps are included in the execution plan) . I want to explore the steps
>later, at this point I just want to compare the two queries as a whole.
>What is the best way of going about this? Regards, Chris.
>|||Chris,
Put both queries in the same query window and run them. Then the
percentiles will reflect both queries. Therefore, if query1 and its
functions use 25% and query2 and its functions use 75%, that suggests that
query1 is more efficient. However, UDFs (and some other functions) do
degrade the reliability of these numbers. Still, it is a good first take.
However, running both queries repeatly and getting a set of actual execution
times for each will, in the final analysis, be a more accurate measure of
the queries.
RLF
"Chris" <nospam@.nospam.com> wrote in message
news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
>I am creating an SP. I want to compare alternative ways of writing the
>query to see which one is the most efficient. I am trying to use the
>execution plan but it doesn't give an overall cost of the query so I can
>compare it to the alternative query. Rather I get a break down of the
>various steps (this query uses functions which produce aggregates and these
>steps are included in the execution plan) . I want to explore the steps
>later, at this point I just want to compare the two queries as a whole.
>What is the best way of going about this? Regards, Chris.
>|||The wording is a bit unclear. My SP could potentially contain a variety of
variables obtained from sub queries etc. This means there will be more than
just two queries to compare. How would you deal with a more complex scenario
with SP's that use temp tables or subqueries etc.
Regards, Chris.
"Russell Fields" <russellfields@.nomail.com> wrote in message
news:emDSzebuHHA.4916@.TK2MSFTNGP04.phx.gbl...
> Chris,
> Put both queries in the same query window and run them. Then the
> percentiles will reflect both queries. Therefore, if query1 and its
> functions use 25% and query2 and its functions use 75%, that suggests that
> query1 is more efficient. However, UDFs (and some other functions) do
> degrade the reliability of these numbers. Still, it is a good first take.
> However, running both queries repeatly and getting a set of actual
> execution times for each will, in the final analysis, be a more accurate
> measure of the queries.
> RLF
> "Chris" <nospam@.nospam.com> wrote in message
> news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
>>I am creating an SP. I want to compare alternative ways of writing the
>>query to see which one is the most efficient. I am trying to use the
>>execution plan but it doesn't give an overall cost of the query so I can
>>compare it to the alternative query. Rather I get a break down of the
>>various steps (this query uses functions which produce aggregates and
>>these steps are included in the execution plan) . I want to explore the
>>steps later, at this point I just want to compare the two queries as a
>>whole. What is the best way of going about this? Regards, Chris.
>|||On Jun 29, 1:02 am, "Chris" <nos...@.nospam.com> wrote:
> The wording is a bit unclear. My SP could potentially contain a variety of
> variables obtained from sub queries etc. This means there will be more than
> just two queries to compare. How would you deal with a more complex scenario
> with SP's that use temp tables or subqueries etc.
> Regards, Chris.
> "Russell Fields" <russellfie...@.nomail.com> wrote in message
> news:emDSzebuHHA.4916@.TK2MSFTNGP04.phx.gbl...
>
> > Chris,
> > Put both queries in the same query window and run them. Then the
> > percentiles will reflect both queries. Therefore, if query1 and its
> > functions use 25% and query2 and its functions use 75%, that suggests that
> > query1 is more efficient. However, UDFs (and some other functions) do
> > degrade the reliability of these numbers. Still, it is a good first take.
> > However, running both queries repeatly and getting a set of actual
> > execution times for each will, in the final analysis, be a more accurate
> > measure of the queries.
> > RLF
> > "Chris" <nos...@.nospam.com> wrote in message
> >news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
> >>I am creating an SP. I want to compare alternative ways of writing the
> >>query to see which one is the most efficient. I am trying to use the
> >>execution plan but it doesn't give an overall cost of the query so I can
> >>compare it to the alternative query. Rather I get a break down of the
> >>various steps (this query uses functions which produce aggregates and
> >>these steps are included in the execution plan) . I want to explore the
> >>steps later, at this point I just want to compare the two queries as a
> >>whole. What is the best way of going about this? Regards, Chris.- Hide quoted text -
> - Show quoted text -
Make each version of your SP. Then capture in profiler
SP:StatementCompleted
with read,writes,duration,CPU
You need to clear database and procedure cache before each
execution of SP to have accurate values
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
Compare Queries
to see which one is the most efficient. I am trying to use the execution
plan but it doesn't give an overall cost of the query so I can compare it to
the alternative query. Rather I get a break down of the various steps (this
query uses functions which produce aggregates and these steps are included
in the execution plan) . I want to explore the steps later, at this point I
just want to compare the two queries as a whole. What is the best way of
going about this? Regards, Chris.What I'd do to start is to fire up SQL Server Profiler, turn on the
SQL:BatchCompleted event, and collect the Reads, Writes, CPU, and Duration
columns. Run your queries and compare the output...
Adam Machanic
SQL Server MVP
Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220
"Chris" <nospam@.nospam.com> wrote in message
news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
>I am creating an SP. I want to compare alternative ways of writing the
>query to see which one is the most efficient. I am trying to use the
>execution plan but it doesn't give an overall cost of the query so I can
>compare it to the alternative query. Rather I get a break down of the
>various steps (this query uses functions which produce aggregates and these
>steps are included in the execution plan) . I want to explore the steps
>later, at this point I just want to compare the two queries as a whole.
>What is the best way of going about this? Regards, Chris.
>|||Chris,
Put both queries in the same query window and run them. Then the
percentiles will reflect both queries. Therefore, if query1 and its
functions use 25% and query2 and its functions use 75%, that suggests that
query1 is more efficient. However, UDFs (and some other functions) do
degrade the reliability of these numbers. Still, it is a good first take.
However, running both queries repeatly and getting a set of actual execution
times for each will, in the final analysis, be a more accurate measure of
the queries.
RLF
"Chris" <nospam@.nospam.com> wrote in message
news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
>I am creating an SP. I want to compare alternative ways of writing the
>query to see which one is the most efficient. I am trying to use the
>execution plan but it doesn't give an overall cost of the query so I can
>compare it to the alternative query. Rather I get a break down of the
>various steps (this query uses functions which produce aggregates and these
>steps are included in the execution plan) . I want to explore the steps
>later, at this point I just want to compare the two queries as a whole.
>What is the best way of going about this? Regards, Chris.
>|||The wording is a bit unclear. My SP could potentially contain a variety of
variables obtained from sub queries etc. This means there will be more than
just two queries to compare. How would you deal with a more complex scenario
with SP's that use temp tables or subqueries etc.
Regards, Chris.
"Russell Fields" <russellfields@.nomail.com> wrote in message
news:emDSzebuHHA.4916@.TK2MSFTNGP04.phx.gbl...
> Chris,
> Put both queries in the same query window and run them. Then the
> percentiles will reflect both queries. Therefore, if query1 and its
> functions use 25% and query2 and its functions use 75%, that suggests that
> query1 is more efficient. However, UDFs (and some other functions) do
> degrade the reliability of these numbers. Still, it is a good first take.
> However, running both queries repeatly and getting a set of actual
> execution times for each will, in the final analysis, be a more accurate
> measure of the queries.
> RLF
> "Chris" <nospam@.nospam.com> wrote in message
> news:emgxeRbuHHA.3796@.TK2MSFTNGP02.phx.gbl...
>|||On Jun 29, 1:02 am, "Chris" <nos...@.nospam.com> wrote:
> The wording is a bit unclear. My SP could potentially contain a variety of
> variables obtained from sub queries etc. This means there will be more tha
n
> just two queries to compare. How would you deal with a more complex scenar
io
> with SP's that use temp tables or subqueries etc.
> Regards, Chris.
> "Russell Fields" <russellfie...@.nomail.com> wrote in message
> news:emDSzebuHHA.4916@.TK2MSFTNGP04.phx.gbl...
>
>
>
>
>
>
> - Show quoted text -
Make each version of your SP. Then capture in profiler
SP:StatementCompleted
with read,writes,duration,CPU
You need to clear database and procedure cache before each
execution of SP to have accurate values
DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE
Thursday, March 8, 2012
Compare csv file layout to SQL Server table
I am creating a facility whereby you can select a source file (.csv), and a target (SQL table).
Then I call a DTS to copy the csv file to the SQL table.
How can I validate that the two files have the same number of columns ?
Moved to SQL Server Tools forum|||In order to check the number of rows you need to work on DTS with workflow, refer to books online for more information and you can schedule the DTS package a SQLagent job by right-click on selected package.|||I need to validate that the correct file has been selected before scheduling the DTS package. This is why I need to do it in my VB.Net Windows application.|||You can write a custom Script Task inside the SSIS package that will open the file and check how many columns it has. You can utilize Excel object model for that or just go with regular .NET IO libraries.
Hope that helps.
Maciek Sarnowicz
Compare csv file layout to SQL Server table
I am creating a facility whereby you can select a source file (.csv), and a target (SQL table).
Then I call a DTS to copy the csv file to the SQL table.
How can I validate that the two files have the same number of columns ?
Moved to SQL Server Tools forum|||In order to check the number of rows you need to work on DTS with workflow, refer to books online for more information and you can schedule the DTS package a SQLagent job by right-click on selected package.|||I need to validate that the correct file has been selected before scheduling the DTS package. This is why I need to do it in my VB.Net Windows application.|||You can write a custom Script Task inside the SSIS package that will open the file and check how many columns it has. You can utilize Excel object model for that or just go with regular .NET IO libraries.
Hope that helps.
Maciek Sarnowicz
Friday, February 24, 2012
common header - AGAIN -
What are the recomendations for creating common reusable report header? I
have many reports and all have common header - company logo (image), report
name, date, pagevount, etc. I need to have the header in one place, so if I
edit the header, it chandes in all the reports.
Is there any way to do this?
Thanks for any suggestion.
MykytaPS subreport does not work - can't put subreport on header/footer
"my" wrote:
> I have seen this question few times but could not find the answer.
> What are the recomendations for creating common reusable report header? I
> have many reports and all have common header - company logo (image), report
> name, date, pagevount, etc. I need to have the header in one place, so if I
> edit the header, it chandes in all the reports.
> Is there any way to do this?
> Thanks for any suggestion.
> Mykyta|||We created an Add-In to write into the rdl. In lue of sub-reports in page
headers, it's an ok solution.
"my" wrote:
> PS subreport does not work - can't put subreport on header/footer
> "my" wrote:
> > I have seen this question few times but could not find the answer.
> >
> > What are the recomendations for creating common reusable report header? I
> > have many reports and all have common header - company logo (image), report
> > name, date, pagevount, etc. I need to have the header in one place, so if I
> > edit the header, it chandes in all the reports.
> > Is there any way to do this?
> >
> > Thanks for any suggestion.
> >
> > Mykyta|||Would you share exactly what you have done?
--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Tad" <Tad@.discussions.microsoft.com> wrote in message
news:1152F1D8-3537-4BA9-B380-29F02E7D7DDB@.microsoft.com...
> We created an Add-In to write into the rdl. In lue of sub-reports in page
> headers, it's an ok solution.
> "my" wrote:
> > PS subreport does not work - can't put subreport on header/footer
> >
> > "my" wrote:
> >
> > > I have seen this question few times but could not find the answer.
> > >
> > > What are the recomendations for creating common reusable report
header? I
> > > have many reports and all have common header - company logo (image),
report
> > > name, date, pagevount, etc. I need to have the header in one place, so
if I
> > > edit the header, it chandes in all the reports.
> > > Is there any way to do this?
> > >
> > > Thanks for any suggestion.
> > >
> > > Mykyta|||I would also be interested in this.
"Tad" <Tad@.discussions.microsoft.com> wrote in message
news:1152F1D8-3537-4BA9-B380-29F02E7D7DDB@.microsoft.com...
> We created an Add-In to write into the rdl. In lue of sub-reports in page
> headers, it's an ok solution.
> "my" wrote:
>> PS subreport does not work - can't put subreport on header/footer
>> "my" wrote:
>> > I have seen this question few times but could not find the answer.
>> >
>> > What are the recomendations for creating common reusable report header?
>> > I
>> > have many reports and all have common header - company logo (image),
>> > report
>> > name, date, pagevount, etc. I need to have the header in one place, so
>> > if I
>> > edit the header, it chandes in all the reports.
>> > Is there any way to do this?
>> >
>> > Thanks for any suggestion.
>> >
>> > Mykyta
Sunday, February 19, 2012
Common Database Management Info
I'm creating an application that will allow users to view information about
certain SQL Server instances and Databases.
Are there some system stored procs or tables where I can find the following
information.
Server Collation
Product Version e.g. 8.00
Product e.g. Standard Edition, Enterprise Edition
Database Names
Collation
Last DB BackUp
Size
Space Available
Owner
Recovery Model
No of Users
Thanksfor server information use
sp_server_info
For the database use it this way
sp_helpdb '<db name>'
Hope this helps.
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/
"Neil" wrote:
> Hi,
> I'm creating an application that will allow users to view information abou
t
> certain SQL Server instances and Databases.
> Are there some system stored procs or tables where I can find the followin
g
> information.
> Server Collation
> Product Version e.g. 8.00
> Product e.g. Standard Edition, Enterprise Edition
> Database Names
> Collation
> Last DB BackUp
> Size
> Space Available
> Owner
> Recovery Model
> No of Users
> Thanks
>
Friday, February 10, 2012
COMException when creating SSIS Package / DataSource
to create a IntegrationServices project.
When i want to create a DataSource, i can specify the connection, but
when i click on finish the screen is not closed whatever info/name/etc.
i fill in. I can only cancel the dialog box.
When i add a new SSIS package i get this exception:
===================================
Error creating package (Microsoft Visual Studio)
===================================
Exception of type 'System.Runtime.InteropServices.COMException' was
thrown. (Microsoft.DataWarehouse.VsIntegration)
Program Location:
at
Microsoft.DataWarehouse.VsIntegration.Shell.Project.FileProjectHierarchy
.OnHierarchyNodeAppending(IHierarchyNode node, IHierarchyNode
parentNode)
at
Microsoft.DataWarehouse.VsIntegration.Hierarchy.Hierarchy.Add(IHierarchy
Node node, IHierarchyNode parentNode)
at
Microsoft.DataWarehouse.VsIntegration.Shell.Project.FileProjectHierarchy
.AddExistingFileToProject(IFileProjectNode& parentNode, String name,
String fullPath, VSADDITEMOPERATION dwAddItemOperation)
at
Microsoft.DataWarehouse.VsIntegration.Shell.Project.FileProjectHierarchy
.AddNewFileToProject(IFileProjectNode& parentNode, String name, String
sourceFullPath, VSADDITEMOPERATION dwAddItemOperation)
at
Microsoft.DataWarehouse.VsIntegration.Shell.Project.Extensibility.Projec
tItemsExt.AddFromTemplate(String FileName, String Name)
at
Microsoft.DataTransformationServices.Wizards.WizardUtils.AddProjectItemF
romTemplate(WizardInputs wizardInputs, String path, String itemName,
Boolean activateWindow)
at
Microsoft.DataTransformationServices.Wizards.Dts90WizardSaveNewTask.AddP
ackageToProject(Package package, String itemName)
at
Microsoft.DataTransformationServices.Wizards.Dts90WizardSaveNewTask.Crea
teAndAddPackageToProject()
at
Microsoft.DataTransformationServices.Wizards.Dts90WizardSaveNewTask.Exec
ute(WizardInputs wizardInputs)
Any idea what's causing this? ( i reinstalled SQL 2005/SSIS several
times)
Thanks
Marco
Do you use any third-party add-ins for Visual Studio?
Do you use source control integration with Visual Studio,
if yes - which source control provider?|||Yes. i'm using DevExpress CodeRush/Refactor... no source control integration.
Thanks
Marco|||Could you please try removing the DevExpress add-in and see if this fixes the situation? Looking at our code - we are simply notifying environment about new file, and some external code subscribed to this event reports failure, thus cancelling adding a new file. Since it does not happen normally, I suspect the DevExpress add-in causes this.
I'll try to get my hands on the DevExpress CodeRush/Refactor to check what's going on.|||Thanks!
Disabling fixed the problem.. I will send my errorreport also to DevExpress.
Marco
come on SQLdatareader....read....read....read...ok help.
Thanks again.
Imports System.data.sqlclient
Imports System.Data
Public Class login2
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
Me.SqlCommand1 = New System.Data.SqlClient.SqlCommand
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "server=LAWORKSTATION;user id=sa;database=test;password=t3st"
'
'SqlCommand1
'
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE (email = 'txtusername.text')"
Me.SqlCommand1.Connection = Me.SqlConnection1
End Sub
Protected WithEvents lblUsername As System.Web.UI.WebControls.Label
Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Protected WithEvents SqlCommand1 As System.Data.SqlClient.SqlCommand
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
SqlConnection1.Open()
Dim dr As SqlDataReader = SqlCommand1.ExecuteReader
If dr.Read() Then
If dr("password").ToString = txtPassword.Text Then
lblMessage.Text = "login successful"
Else
lblMessage.Text = "Wrong password"
End If
Else
lblMessage.Text = "Please register"
End If
dr.Close()
SqlConnection1.Close()
End Sub
End ClassTake a look at this line here:
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE (email = 'txtusername.text')"
What you are asking here is to retrieve all [pass] fields from the rows in [Customer] table where the value of field [email] is "txtusername.text".
What you probably meant to ask is retrieve all [pass] fields from the rows in [Customer] table where the value of field [email] is the same as the value of txtUsername.Text.
Which should translate into command text like so:
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE (email = '" & txtUsername.Text & "')"
That should work. However, now you need to think about the possibility of a SQL injection attacks. Suppose somebody enters this is string as an email (single qoutes are significant):
'); DELETE Customer; SELECT ('
As you might've guessed, that would neatly zap your Customer table.
There are many ways to protect yourself from such an attack. Here's one possibility:
Create a function like this (pardon my VB.NET, it's rusty):
Function Tick(s As String)
Tick = s.Replace("'", "''")
End Function
And modify the command text assignment line like so:
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE (email = '" & Tick(txtUsername.Text) & "')"
That would make sure that any attempts to close the tick marks in the statement would fail.
Anyways, happy coding!|||Better than trying to outsmart folks trying SQL Injection attacks,use parameters.|||OK, for testing purposes I did change it with querybuilder and this is what VS2003 enters in for the command:
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE (email = ' "" & txtUsername.text & "" ')"
It enters double "" by default...and it doesn't work.
I then manually entered you line precisely as typed above:
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE (email = '" & txtUsername.Text & "')"
I've also tried:
Me.SqlCommand1.CommandText = "SELECT pass FROM Customer WHERE email = '" & txtUsername.Text & "'"
It still doesn't work. The response is always "Please register".
I've tested the connection and it works. The table data is correct when I enter in the right login and password but the prompt is nothing but "Please register". This page without the code behind works flawlessly. I'm stumped.|||Erm unless I'm reading this incorrectly isn't the result of txtUserName.text ALWAYS empty? You're populating the command string before anyone's typed in their name.
Try moving the ...CommandText = ... line just before the ExecuteReader|||Good catch, pkr!
He is right. Although we've fixed the one problem, there's still the other one present: InitializeComponent is called (and the CommandText property initialized) at the time the page is loading, which prevents the actual submitted value of txtUsername from ever being submitted.
As "douglas.reily" noted, time to call in parameters for help.
First, let's modify our query to this:
SELECT pass FROM Customer WHERE (email = @.email)
Then, just before you call ExecuteReader, add parameter to the command:
Me.SqlCommand1.Parameters.Add(New SqlParameters("@.email", textUsername.Text)
That should do it.|||I think you are reading it incorrectly. SQLdatareader runs SQLcommand1 which, in turn, runs the query for getting the login and password. SQLdatareader is run only when the user has clicked on the "submit" button and txtUsername.text is populated then. At least, I think this is how it happens.
I'm going to move to stored procedures and parameters instead but this is just bugging me that this page works perfectly fine without a code behind page but it has problems when I redid it with VS2003.|||OK, I've made some changes and added a parameter. In the "click" sub I've populated the parameter like this:
SqlCommand1.Parameters.Item("@.email").Value = txtUsername.Text
This seems to work. Thanks for all your help.|||Glad it's working. But out of interest does it still work in you put the ....value = txtUserName.Text... line back to it's original position. i.e. not in the same function as the button event?
Come on MSFT guys - I can't be the only one
Problems with Document Map creating a side scroll to get to Up/Down Scroll for the Map itself. There are two scroll bars once you side scroll over in the map window. Anyone seen this or know how to fix?
Anyone know how to change any of the properties on the Document Map or map window?
Please Help!! This is soooooo frustrating
i can′t image that, could you send a screenshot or something like that ?HTH, Jens Suessmeyer.
http://www.sqlserver2005.de|||
Can't exactly figure out how to do that