SQL Server, views usage count - tsql

My scenario is like this:
I have a couple of views in my databse (SQL Server 2005).
These views are queried from Excel across the organization.
My goal is to identify those views which have not been used by anyone for a long time.
Is there a way to count the number of times a view has been requested since a specific date?
Thanks
Avi

You can use following query to get some queries those executed. You can place "Like" operator in dest.text field to check for views.
SELECT deqs.last_execution_time AS [Time], dest.text AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DES

I think a combination of DMVs and sysobjects could tell you this. This should hopefully show you all queries run that refer to a view, the name of the view, when it was last run etc.
SELECT s2.text AS Query,
so.name AS ViewName,
creation_time,
last_execution_time,
execution_count
FROM sys.dm_exec_query_stats AS s1
CROSS APPLY sys.Dm_exec_sql_text(sql_handle) AS s2
INNER JOIN sys.objects so
ON so.object_id = s2.objectid
AND so.type = 'V'

I don't think that you'll be able to do this unless you are running a trace 24/7. You could turn on auditing in order to monitor it. But, it would be a big task for however has to read through the logs.

Related

Converting complex query with inner join to tableau

I have a query like this, which we use to generate data for our custom dashboard (A Rails app) -
SELECT AVG(wait_time) FROM (
SELECT TIMESTAMPDIFF(MINUTE,a.finished_time,b.start_time) wait_time
FROM (
SELECT max(start_time + INTERVAL avg_time_spent SECOND) finished_time, branch
FROM mytable
WHERE name IN ('test_name')
AND status = 'SUCCESS'
GROUP by branch) a
INNER JOIN
(
SELECT MIN(start_time) start_time, branch
FROM mytable
WHERE name IN ('test_name_specific')
GROUP by branch) b
ON a.branch = b.branch
HAVING avg_time_spent between 0 and 1000)t
GROUP BY week
Now I am trying to port this to tableau, and I am not being able to find a way to represent this data in tableau. I am stuck at how to represent the inner group by in a calculated field. I can also try to just use a custom sql data source, but I am already using another data source.
columns in mytable -
start_time
avg_time_spent
name
branch
status
I think this could be achieved new Level Of Details formulas, but unfortunately I am stuck at version 8.3
Save custom SQL for rare cases. This doesn't look like a rare case. Let Tableau generate the SQL for you.
If you simply connect to your table, then you can usually write calculated fields to get the information you want. I'm not exactly sure why you have test_name in one part of your query but test_name_specific in another, so ignoring that, here is a simplified example to a similar query.
If you define a calculated field called worst_case_test_time
datediff(min(start_time), dateadd('second', max(start_time), avg_time_spent)), which seems close to what your original query says.
It would help if you explained what exactly you are trying to compute. It appears to be some sort of worst case bound for avg test time. There may be an even simpler formula, but its hard to know without a little context.
You could filter on status = "Success" and avg_time_spent < 1000, and place branch and WEEK(start_time) on say the row and column shelves.
P.S. Your query seems a little off. Don't you need an aggregation function like MAX or AVG after the HAVING keyword?

Query executes in SSMS but crashes BIDS

I have the following query, which executes perfectly in SSMS. However, when I paste the code into a newly created dataset and click "execute", BIDS crashes and closes without notice - any suggestion as to the reason?
Code:
SELECT MSP_EpmProject_UserView.ProjectOwnerName,
MSP_EpmProject_UserView.ProjectUID,
CurrentActivities.Activities_Current
FROM MSP_EpmProject_UserView
Left Join
(Select t1.projectUID, STUFF(
(Select ', ' + t2.TaskName
From (SELECT MSP_EpmProject_UserView.ProjectUID, MSP_EpmTask_UserView.TaskName
FROM MSP_EpmAssignmentByDay_UserView INNER JOIN MSP_EpmTask_UserView ON MSP_EpmAssignmentByDay_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID INNER JOIN MSP_EpmProject_UserView on MSP_EpmAssignmentByDay_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID
WHERE (MSP_EpmAssignmentByDay_UserView.TimeByDay = #refDate) AND MSP_EpmProject_UserView.[Project Departments] = #prjDep) t2
Where t1.ProjectUID = t2.ProjectUID
Order by t2.TaskName
For XML Path(''), Type).value('.','varchar(max)'),1,2,'') as Activities_Current
From
(SELECT MSP_EpmProject_UserView.ProjectUID, MSP_EpmTask_UserView.TaskName
FROM MSP_EpmAssignmentByDay_UserView INNER JOIN MSP_EpmTask_UserView ON MSP_EpmAssignmentByDay_UserView.TaskUID = MSP_EpmTask_UserView.TaskUID INNER JOIN MSP_EpmProject_UserView on MSP_EpmAssignmentByDay_UserView.ProjectUID = MSP_EpmProject_UserView.ProjectUID
WHERE (MSP_EpmAssignmentByDay_UserView.TimeByDay = #refDate) AND MSP_EpmProject_UserView.[Project Departments] = #prjDep) t1
Group by t1.ProjectUID) CurrentActivities On CurrentActivities.ProjectUID = MSP_EpmProject_UserView.ProjectUID
WHERE (MSP_EpmProject_UserView.[Project Departments] = #prjDep)
I would try to create a view and see if that still crashes. BIDS is not meant to be SSMS and it's interpretation of the XML parsing may be blowing it up. You are translating values from a query using 'for xml', with 'type' which gets rid of the root element of the xml, then extending it, then nesting it, then nesting that, then left joining that, then you have a regular sql expression. BIDS is not meant to do a lot of dynamic SQL, intense CLR, and xml node or xquery expressions nesting things in my experience. Everytime I have thrown lot's of XML, table variables, recursive CTE's, dyanmic SQL... it blows up. It's interpreter is blowing up I am guessing as it does not have the resources SSMS does to evaluate the expression and gives up in the form of a blow up.
My thought on doing the view is that the interpretation will be then forced on the SQL engine in the database to perform it's logic and execution plan and then return the results. BIDS will just know it is a call to perform but not have to reason out the logic. In my recollection SSRS with BIDS on 2008 has many issues with complex query logic and SSAS cubes.
This may not work but I know putting things to views, functions, or procs will often times fix issues with complex logic the engine has to perform.
I managed to work my way around this by just pasting the code into the dataset, updating the fields, NOT clicking the "query designer" and by manually adding the parameters BIDS did not detect automatically (in my case an INT parameter for DATEADD() ). When I then close the dataset windows and use the dataset in my report, everything works fine

ZF_DB: how to make simple select for two or more tables without join?

Example:
SELECT `cat`.`id_catalog`, COUNT(parent.id_catalog) - 1) AS `level` FROM `tbl_catalog` AS `cat`, `tbl_catalog` AS `parent` WHERE (cat.`left` BETWEEN parent.`left` AND parent.`right`) GROUP BY `cat`.`id_catalog` ORDER BY `cat`.`left` ASC
It doesn't seem to work if it use ZF. ZF create this query with join only. How to create the select without join in ZF_DB.
By the way may be I do smth wrong in this query. It is simple nested set DB with parent, left and right fields. Perhaps there are another way to use join to get deep for some node. Anyway it would be interesting to get answer for both way:)
Thanks in advance to all who looks it through:)

iphone sqlite query optimization

I am currently using this sqlite query in my application. Two tables are used in this query.....
UPDATE table1 set visited = (SELECT COUNT(DISTINCT table1.itemId) from 'table2' WHERE table2.itemId = table1.itemId AND table2.sessionId ='eyoge2avao');
It is working correct.... My problem is it is taking around 10 seconds to execute this query and retrieve the result..... Don't know what to do... Almost all other process are in right way.. So it seems the problem is with this query formation...
Plz someone help with how to optimize this query....
Regards,
Brian
Make sure you have indexes on the following (combinations of) fields:
table1.itemId
(This will speed up the DISTINCT clause, since the itemId will already be in the correct order).
table2.itemId, table2.sessionId
This will speed up the WHERE clause of your SELECT statement.
How many rows are there in these tables?
Aso try doing an EXPLAIN on your SELECT command to see if it gives you any helpful advice.

Optimising (My)SQL Query

I usually use ORM instead of SQL and I am slightly out of touch on the different JOINs...
SELECT `order_invoice`.*
, `client`.*
, `order_product`.*
, SUM(product.cost) as net
FROM `order_invoice`
LEFT JOIN `client`
ON order_invoice.client_id = client.client_id
LEFT JOIN `order_product`
ON order_invoice.invoice_id = order_product.invoice_id
LEFT JOIN `product`
ON order_product.product_id = product.product_id
WHERE (order_invoice.date_created >= '2009-01-01')
AND (order_invoice.date_created <= '2009-02-01')
GROUP BY `order_invoice`.`invoice_id`
The tables/ columns are logically names... it's an shop type application... the query works... it's just very very slow...
I use the Zend Framework and would usually use Zend_Db_Table_Row::find(Parent|Dependent)Row(set)('TableClass') but I have to make lots of joins and I thought it'll improve performance by doing it all in one query instead of hundreds...
Can I improve the above query by using more appropriate JOINs or a different implementation? Many thanks.
The query is wrong, the GROUP BY is wrong. All columns in the SELECT-part that are not in an aggregate function, have to be in the GROUP BY. You mention only one column.
Change the SQL Mode, set it to ONLY_FULL_GROUP_BY.
When this is done and you have a correct query, use EXPLAIN to find out how the query is executed and what indexes are used. Then start optimizing.