Hello World!
The following query takes more than one hour to complete. Assuming the
tables are properly indexed, if this can be tuned for quicker return?
Besides, I wonder if WHERE EXISTS is always/usually more efficient than
INNER JOIN? Can WHERE EXISTS be implemented in this case and how? Many THANK
S.
SELECT DISTINCT count(*)
FROM FACT_SALES fs INNER JOIN DIM_DEPARTMENT dt
ON fs.Dept_ID=dt.Dept_ID INNER JOIN DIM_DATE dd
ON fs.Date_ID=dd.Date_ID INNER JOIN DIM_MINUTE dm
ON fs.Minute_ID=dm.Minute_ID INNER JOIN TLOG t
ON
Convert(datetime,SUBSTRING(t.[Date],3,2)+'/'+SUBSTRING(t.[Date],5,2)+'/20'+LEFT(t.[Date],2))=d
d.DATE_FULL
AND dm.MINUTE_NUMBER_24=t.[Time]
AND dt.HQ_ID=t.HQ_ID
AND dt.Store_ID=t.Store_ID
AND dt.Dept_ID=t.Dept_ID
AND Convert(bigint,fs.Invoice_No)=(
CASE WHEN t.TransactionType='CreditCard' THEN
Convert(bigint,LEFT(t.JoinKey,4) )
ELSE Convert(bigint,t.JoinKey)
END
)
WHERE ISNUMERIC(fs.Invoice_No) = 1 AND ISNUMERIC(LEFT(t.JoinKey,4)) = 1Hi
Have a look at execution plan of the query. Does it use any indexes?
I have my doubt ,because in WHERE condition a LEFT function would not
probably allow to optimyzer to use the index.
How is selective the columns that participate in WHERE condition?
"C TO" <CTO@.discussions.microsoft.com> wrote in message
news:35BD39C0-F97C-4A56-AC84-125B55D8F488@.microsoft.com...
> Hello World!
> The following query takes more than one hour to complete. Assuming the
> tables are properly indexed, if this can be tuned for quicker return?
> Besides, I wonder if WHERE EXISTS is always/usually more efficient than
> INNER JOIN? Can WHERE EXISTS be implemented in this case and how? Many
THANKS.
>
> SELECT DISTINCT count(*)
> FROM FACT_SALES fs INNER JOIN DIM_DEPARTMENT dt
> ON fs.Dept_ID=dt.Dept_ID INNER JOIN DIM_DATE dd
> ON fs.Date_ID=dd.Date_ID INNER JOIN DIM_MINUTE dm
> ON fs.Minute_ID=dm.Minute_ID INNER JOIN TLOG t
> ON
>
Convert(datetime,SUBSTRING(t.[Date],3,2)+'/'+SUBSTRING(t.[Date],5,2)+'/20'+L
EFT(t.[Date],2))=dd.DATE_FULL
> AND dm.MINUTE_NUMBER_24=t.[Time]
> AND dt.HQ_ID=t.HQ_ID
> AND dt.Store_ID=t.Store_ID
> AND dt.Dept_ID=t.Dept_ID
> AND Convert(bigint,fs.Invoice_No)=(
> CASE WHEN t.TransactionType='CreditCard' THEN
> Convert(bigint,LEFT(t.JoinKey,4) )
> ELSE Convert(bigint,t.JoinKey)
> END
> )
> WHERE ISNUMERIC(fs.Invoice_No) = 1 AND ISNUMERIC(LEFT(t.JoinKey,4)) = 1|||Hi
Have you tried to look at the execution plan? You should get a good idea
where/how its spending its time. Optimizing without the plan is rather
difficult.
You can use the SQL Query Analyzer that comes with the Enterprise
Manager's client-side tool set.
-David
C TO wrote:
> Hello World!
> The following query takes more than one hour to complete. Assuming the
> tables are properly indexed, if this can be tuned for quicker return?
> Besides, I wonder if WHERE EXISTS is always/usually more efficient than
> INNER JOIN? Can WHERE EXISTS be implemented in this case and how? Many THA
NKS.
>
> SELECT DISTINCT count(*)
> FROM FACT_SALES fs INNER JOIN DIM_DEPARTMENT dt
> ON fs.Dept_ID=dt.Dept_ID INNER JOIN DIM_DATE dd
> ON fs.Date_ID=dd.Date_ID INNER JOIN DIM_MINUTE dm
> ON fs.Minute_ID=dm.Minute_ID INNER JOIN TLOG t
> ON
> Convert(datetime,SUBSTRING(t.[Date],3,2)+'/'+SUBSTRING(t.[Date],5,2)+'/20'+LEFT(t.[Date],2))
=dd.DATE_FULL
> AND dm.MINUTE_NUMBER_24=t.[Time]
> AND dt.HQ_ID=t.HQ_ID
> AND dt.Store_ID=t.Store_ID
> AND dt.Dept_ID=t.Dept_ID
> AND Convert(bigint,fs.Invoice_No)=(
> CASE WHEN t.TransactionType='CreditCard' THEN
> Convert(bigint,LEFT(t.JoinKey,4) )
> ELSE Convert(bigint,t.JoinKey)
> END
> )
> WHERE ISNUMERIC(fs.Invoice_No) = 1 AND ISNUMERIC(LEFT(t.JoinKey,4)) = 1|||Your query relies on doing calculations to join between tables and to filter
results. Because of this, SQL Server cannot use indexes to do quick lookups
and will instead do a full table scan, running the calculation on each row
to try to get a match. Try taking the calculations from the query and
perhaps using them to produce indexable calculated column(s).
In news:35BD39C0-F97C-4A56-AC84-125B55D8F488@.microsoft.com,
C TO <CTO@.discussions.microsoft.com> said:
> Hello World!
> The following query takes more than one hour to complete. Assuming the
> tables are properly indexed, if this can be tuned for quicker return?
> Besides, I wonder if WHERE EXISTS is always/usually more efficient
> than
> INNER JOIN? Can WHERE EXISTS be implemented in this case and how?
> Many THANKS.
>
> SELECT DISTINCT count(*)
> FROM FACT_SALES fs INNER JOIN DIM_DEPARTMENT dt
> ON fs.Dept_ID=dt.Dept_ID INNER JOIN DIM_DATE dd
> ON fs.Date_ID=dd.Date_ID INNER JOIN DIM_MINUTE dm
> ON fs.Minute_ID=dm.Minute_ID INNER JOIN TLOG t
> ON
>
Convert(datetime,SUBSTRING(t.[Date],3,2)+'/'+SUBSTRING(t.[Date],5,2)+'/20'+L
EFT(t.[Date],2))=dd.DATE_FULL
> AND dm.MINUTE_NUMBER_24=t.[Time]
> AND dt.HQ_ID=t.HQ_ID
> AND dt.Store_ID=t.Store_ID
> AND dt.Dept_ID=t.Dept_ID
> AND Convert(bigint,fs.Invoice_No)=(
> CASE WHEN t.TransactionType='CreditCard' THEN
> Convert(bigint,LEFT(t.JoinKey,4) )
> ELSE Convert(bigint,t.JoinKey)
> END
> )
> WHERE ISNUMERIC(fs.Invoice_No) = 1 AND ISNUMERIC(LEFT(t.JoinKey,4)) =
> 1
Stevesql
Showing posts with label complete. Show all posts
Showing posts with label complete. Show all posts
Friday, March 23, 2012
Monday, March 12, 2012
Query to find SQL activity at specific time?
Ok, please pardon my complete ignorance; but I am extremely SQL challenged. I manage the server right now and I am the closest thing to a DBA at the moment with this server.
Here is the problem. I have a job that runs every hour and until last week it was running without any problems. Now, once a day it hangs, and I end up having to restart sql to get it to be functional again. I guess I could just kill the spid, but regardless, it's fails.
It doesn't happen at the same time each day. I thought it was from the backups doing a SQL backup job, but I eliminated that possibility. I can't find what is causing the problem, because the SQL logs just stop at the time that this happens. It's like some other job/query is running and taking all the sql server resources. But I can never catch it when it's happening and I have no event logs or SQL logs to go on.
Is there any query that I can run to find what happened around the specific time the job failed to run? I just need to find what process did what when this job stopped working.
Again, I apologize if my lack of SQL knowledge is making me ask dumb questions, but I am really lost.
Thanks in advanced!Without specifics, all you can get is general answers. Go Google SQL Server Black Box and see if that will help you.|||You can use SQL Profiler to run a SQL trace to a system file on the server or a network drive, or to a table on a different SQL instance. That way, you can choose what you want to look at, including users and spid's along with a lot of other data. It's completely customizable. You can also set the size so that it doesn't grow too large and then you can look at the data after the other server has crashed.
It's also important to be sure that you have enough processor, memory and disk space as the first and foremost problem. I've come into a lot of systems that were having "hanging" issues and the servers were bare minimum or below what they needed to be running.
I hope this helps.|||Thank you! I think that will do it. Now I have something to go on.
Here is the problem. I have a job that runs every hour and until last week it was running without any problems. Now, once a day it hangs, and I end up having to restart sql to get it to be functional again. I guess I could just kill the spid, but regardless, it's fails.
It doesn't happen at the same time each day. I thought it was from the backups doing a SQL backup job, but I eliminated that possibility. I can't find what is causing the problem, because the SQL logs just stop at the time that this happens. It's like some other job/query is running and taking all the sql server resources. But I can never catch it when it's happening and I have no event logs or SQL logs to go on.
Is there any query that I can run to find what happened around the specific time the job failed to run? I just need to find what process did what when this job stopped working.
Again, I apologize if my lack of SQL knowledge is making me ask dumb questions, but I am really lost.
Thanks in advanced!Without specifics, all you can get is general answers. Go Google SQL Server Black Box and see if that will help you.|||You can use SQL Profiler to run a SQL trace to a system file on the server or a network drive, or to a table on a different SQL instance. That way, you can choose what you want to look at, including users and spid's along with a lot of other data. It's completely customizable. You can also set the size so that it doesn't grow too large and then you can look at the data after the other server has crashed.
It's also important to be sure that you have enough processor, memory and disk space as the first and foremost problem. I've come into a lot of systems that were having "hanging" issues and the servers were bare minimum or below what they needed to be running.
I hope this helps.|||Thank you! I think that will do it. Now I have something to go on.
Subscribe to:
Posts (Atom)