i am using below query to generate some information of around 7k lines. but the issue is that the query is taking too much time to complete just 7k line. i need to fine tune it.
select mo.CardUId,mo.CarParkUId,mo.PersonUId,mo.CardType,mo.CardNr,pk.LastName company_name,ab.Name carpark_name,
mo.ValidFrom,mo.ValidUntil,mo.LastTccTime,p.LastName customerName,mo.GroupName, mo.Name,p.Type,
mo.LastTccNr,mo.DisplayableCardCarrierNrId serialnr,mo.BlacklistEntry,
mo.GroupNumber from Person p
LEFT outer join vwAllCards mo on p.PersonUId=mo.PersonUId
left outer join Person pk on pk.PersonUId=p.CompanyDepartmentUId
left outer join CarPark ab on ab.CarParkUId=mo.CarParkUId
where p.Type=0 and mo.Name is not null
order by p.LastName
I use the SQL Profiler to auto create the indexes needed for a fine tune query.
Enter in SSMS (SQL Server Management Studio) -> Tools -> SQL Server Profiler.
Click Connect, and then click Run, to start a new trace.
Execute your slow query now, so the profiler can capture and record it.
Stop the query and File -> Save it.
In the menu, click Tools -> Database Engine Tuning advisor.
Click Connect when it opens, and in Workload, select file, and click the first button on the right (binoculars one). Then select the file you have just saved.
Select now the checkbox of your database, and click the "Start Analysis" button (below the "View" and "Action" menu).
Once it finish, open "Action" menu, and click in "Apply Recommendations".
Now you should have a fast and fine tuned query.
use with (nolock)
mo.GroupNumber from Person p with (nolock)
Related
sometimes I have completed the development of a solution on a development instance of a SQL database, after several test cycles, reworks, adjustments, etc.
When is time to move everything in production, I have to be sure that nothing is left behind by mistake (a trigger, a stored procedure).
One helper is to get a simple list of all the objects in the database.
A simple query to get the list of all the objects in your db is the following one:
select * from Sys.objects as o left join sys.schemas as s on o.schema_id = s.schema_id
where is_ms_shipped = 0
order by s.name, o.type, o.name
You can adjust it at your needs, remove fields, and so on.
Few notes:
I have excluded items delivered by Microsoft (is_ms_shipped)
I have joined the main system table with a catalogue of all the objects with the description of the schema. This is helpful to order all the elements
Using SSMS (SQL Server Management Studio), you can easily export the results in Excel, and work on it, add it to the documentation.
I am trying to create a SQL View in SSMS. I am using Views because they are easier to invoke from Power BI than Stored Procedures (especially when no parameters are needed).
I start by writing and testing a SQL SELECT query with an ORDER BY clause.
When I copy and paste my query in the New View:
SSMS adds a TOP (100) PERCENT to my SELECT statement.
Tells me that my ORDER BY clause (which works perfectly well in the SQL SELECT) may not work.
If you click the Help button on the dialog, you are taken to a Microsoft "Oops! No F1 help match was found" page.
My questions are:
Is TOP (100) PERCENT not implied when it is left out of a SQL Select?
Why would a View based on a SQL Select statement not like ORDER BY clauses?
SQL views to not support ORDER BY. For more detail on this, see these other posts:
Create a view with ORDER BY clause
Possible to have an OrderBy in a view?
Order BY is not supported in view in sql server
Why use Select Top 100 Percent?
As #Martin Smith said, your options are one of the following:
Put the ORDER BY in a query that references the view.
SELECT * FROM ViewName ORDER BY [...]
Do the ordering in the Power Query Editor. If you don't have any steps before this sort that break query folding, this should be translated into a native SQL query that gets evaluated on the SQL Server.
I recommend the latter since further steps can also potentially be folded in as well. Specifying your own query does not support query folding.
I have created a report (call it Primary) and a drillthrough (call it Secondary) in Report Builder. Each of these has a SQL statement.
When executed in SQL Server Management Studio, SQL statements work as expected.
However, when the Primary.rdl and Secondary.rdl are uploaded to Report Manager (the web interface in Internet Explorer), they do not generate the correct data when run.
Because of this, I think the problem is not the SQL statements. I think it's something to do with the Report Manager.
Primary SQL statement:
This statement grabs a bunch of user data from multiple tables and checks if their passwords are acceptable. It populates a list of users whose passwords failed the check.
This is pseudocode so pardon inconsistencies in var names
with details as (
select u.userid
, u.password
, u.firstname
, u.lastname
, u.userdescription
, u.status
, u.lastlog
, dbo.IsPassswordAcceptable(u.userid, u.password) as passStatus
from masterListOfUsers as u
)
select d.*, p.datavalue
from details as d
left join passwordDetailList as p
on p.keyvalue = d.passStatus
and p.datatype = 'ERRORMESSAGE'
where d.passStatus <> 1
and d.passStatus <> -5
and d.status = (#USERSTATUS) -- only user ids in use
;
Secondary SQL statement:
This statement is a drillthrough. The person running the report can click on a userID in the above list. A drillthrough is performed where the contact information for that userID is populated.
This is pseudocode so pardon inconsistencies in var names
SELECT
m.userid
, c.address
, c.city
, c.state
, c.zip
, c.cphone
FROM userMasterList AS m
left join userDetailList AS d
ON d.userid = m.userid
left join anotherList as e on d.fullkey = e.fullkey
left join yetAnotherList AS c
WHERE m.userid = #USERID;
Expected result:
When the user runs the Primary, a list of users with bad passwords is populated. Each user's userID can be clicked on, which triggers the Secondary to populate the location/contact info associated with that userID.
Actual result:
On userID click, the Secondary fails to populate any location/contact info associated with the userID. This occurs only sometimes. Other times, it works fine.
I made a list of these "empty" userIDs and ran the Secondary's SQL statement in Management Studio, and it populates all the expected location/contact info.
Solutions I've tried:
I'm absolutely stumped. I've triple-checked the SQL statements and tested them in Management Studio. I've re-uploaded both .rdl files to Report Manager. I've reassigned the Secondary to the Primary via the "Create Linked Report" option in Report Manager AND ALSO in Report Builder's Action > Go To Report option.
What else can I do?
This is not really an answer as such, but a list of things I would work thru in the same situation.
Run SQL Profiler to trace your report session and make sure the query being executed is what you expect. Depending on how parameters are passed to the SQL statements, SSRS will not always do things quite the way you expected.
Check if you can repeat the issue by just running the drill thru report on it's own (not via the primary report)
Determine if the issue is consistent with specific userids? i.e. does user A always fail and User B always work? If the issue is consistent, the issue is most likely to be data related. Check for special characters in the fields that appear to be blank such as chr(13)/chr(10), they may just be forcing the 'real' content onto a new line inside the textbox.
Add some debug info to your report to help identify the issue such as:
a. Edit the dataset query to add some more info from dataset itself SELECT .... , c.addrees, len(c.address) as AddresLen from .... You can add this to a copy of your report
b. Add another textbox that does the same thing but directly in SSRS (e.g. expression would be something like =LEN(Fields!address.Value)). You then have two numbers to compare against what you can see. If the LEN textbox says 20 but the address field appears blank, then special characters could be the issue.
After hours of tinkering, the problem ended up being that the userID was being trimmed of all leading and trailing whitespace by some of the query tools but not by the SQL statements themselves. So when the final report is run in Report Manager, the data is queried with superfluous whitespace, resulting in no data being found.
This issue is resolved when the data points are trimmed.
The fixed Secondary SQL statement:
This is pseudocode so pardon inconsistencies in var names
SELECT
rtrim(m.userid) as userid
, rtrim(c.address) as address
, rtrim(c.city) as city
, rtrim(c.state) as state
, rtrim(c.zip) as zip
, rtrim(c.phone) as phone
FROM userMasterList AS m
left join userDetailList AS d
ON d.userid = m.userid
left join anotherList as e on d.fullkey = e.fullkey
left join yetAnotherList AS c
WHERE ltrim(rtrim(m.userid)) = ltrim(rtrim(#USERID));
I'm trying to create a SQL View that pulls 2 tables together each from a different DB. The SQL works fine in the query editor but when I try to run it as a view all the columns from the MSP_EpmProject table say "Unsupported DataType".
SELECT TOP (200) dbo.Project.ProjectID, dbo.Project.ProjectGUID, dbo.Project.ProjectName, dbo.Project.DefaultBaselineID,
FMM_ProjectServer_Reporting.dbo.MSP_EpmProject.ProjectName AS Expr1
FROM dbo.Project INNER JOIN
FMM_ProjectServer_Reporting.dbo.MSP_EpmProject ON dbo.Project.ProjectGUID = FMM_ProjectServer_Reporting.dbo.MSP_EpmProject.ProjectUID
Check out this bug report - http://connect.microsoft.com/SQLServer/feedback/details/464339/unsupported-data-type-reported-for-supported-data-types-in-nested-query
It appears to be a long standing issue from SQL 2005 which they still havent fixed.
The work around appears to be to not work with your view in design mode, you will have to develop this view manually in query analyzer.
What is the query to backup the stored procedure of a database in SQL Server 2000?
In SQL Server 2000, you can use this query to list out the complete text of stored procedures, they can span multiple rows.
SELECT
o.name,o.id,o.xtype, c.colid, c.text
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c ON o.id = c.id
WHERE o.xtype = 'p'
ORDER BY o.Name,c.colid
I would be easier to use Enterprise Manager to script all the procedures though. In Enterprise Manager, right click on the database to you want to capture all the procedures from. An options list will pop-up, select "All Tasks" then "Generate SQL Script...". A dialogue box will appear, click on "show all", you can then refine the list of objects to script, by using the check boxes. Select the objects on the left side and click on the "Add>>" to move them to the script list. You can set formatting and other options, then click OK when done.
In SQl Server 2005+ you can use this query to list the complete text of all stored procedures, views and functions:
SELECT
LEFT(o.name, 100) AS Object_Name,o.type_desc,m.definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
you can take this output and save it if you like.
However, it is easier to use SQL Server management Studio to script out all procedures.