Speeding Up SQL Query Time
Jul 20, 2005
Consider this SQL Query:
-----------------------------------------------------------------
SELECT c.CASE_NBR, DATEDIFF(d, c.CREATE_DT, GETDATE()) AS Age,
c.AFFD_RCVD, c.PRV_CRD_ISS, x.RegE, x.Type, x.Fraud,
c.CUST_FN + ' ' + c.CUST_LN AS CustFullName,
c.ATM_CKCD_NBR, x.TotalLoss, x.Queue, x.Status,
c.QUEUE AS Expr1, x.CHECK_ACT_NBR, c.CUST_LN, c.SSN,
c.CREATE_DT
FROM (
SELECT TOP 9999999 cl.CASE_NBR, cl.SSN, cl.CREATE_DT,
SUM(cast(TRANS_AMNT AS float)) AS TotalLoss,
glQueue.REFN_NM AS Queue,
glStatus.REFN_NM AS Status,
grRegE.REFN_NM AS RegE, grType.REFN_NM AS Type,
grFraud.REFN_NM AS Fraud, cl.CHECK_ACT_NBR
FROM (
((((T_CASE_LST AS cl LEFT JOIN
T_INCIDENT_LST AS il ON cl.CASE_NBR = il.CASE_NBR)
INNER JOIN T_GNRL_REFN AS glQueue
ON cl.QUEUE = glQueue.REFN_NBR)
INNER JOIN T_GNRL_REFN AS glStatus
ON cl.STATUS_CD = glStatus.REFN_NBR)
INNER JOIN T_GNRL_REFN AS grRegE
ON cl.REGE_CD = grRegE.REFN_NBR)
INNER JOIN T_GNRL_REFN AS grType
ON cl.CASE_TYPE_CD = grType.REFN_NBR
)
INNER JOIN T_GNRL_REFN AS grFraud ON cl.FRAUD_CD =
grFraud.REFN_NBR
WHERE (
((glQueue.REFN_DESC) = 'Queue')
AND ((glStatus.REFN_DESC) = 'STATUS_CD')
AND ((grRegE.REFN_DESC) = 'YesNo')
AND ((grType.REFN_DESC) = 'Fraud_Code')
AND ((cl.STATUS_CD) = 0)
)
GROUP BY cl.CASE_NBR, glQueue.REFN_NM, glStatus.REFN_NM,
grRegE.REFN_NM, grType.REFN_NM, grFraud.REFN_NM,
grFraud.REFN_DESC, cl.CHECK_ACT_NBR,
cl.SSN, cl.CREATE_DT
HAVING (((grFraud.REFN_DESC) = 'YesNo'))
) x
LEFT OUTER JOIN T_CASE_LST c ON x.CASE_NBR = c.CASE_NBR
-----------------------------------------------------------------
1. Is there anything that can be done to speed up the query?
2. This part of the query: ... AND ((cl.STATUS_CD) = 0 ... where the 0
is actually a variable passed in via a VB application. 0 would be new
cases, and normally return around 4000 - 5000 records.
3. The SQL server, Web Server, and users, are all in different states.
4. The time to return this query where cl.STATUS_CD = 0 is about 7 -
12 seconds.
5. Is this a reasonable time for this query? What can be done to
increase the time?
6. The SQL server is indexed on T_CASE_LST.STATUS_CD and
T_INCIDENT_LST.CASE_NBR, but not on any field from T_GNRL_REFN since
T_GNRL_REFN is only a general lookup table, and contains less than 50
records.
7. I've built the query as a stored procedure, and it works, though no
measurable speed increase was obtained.
8. I have not attempted building a view to aid this, as I don't see
that helping... or will it?
9. Well: any ideas?
10. I would gladly rewrite the SQL Query if it could return the same
data faster using another method.
11. Is there a way to accomplish the joins involved with the
T_GNRL_REFN in another manner to make it quicker?
12. Is there a better way to add the values in T_INCIDENT_LST than:
.... SUM(cast(TRANS_AMNT AS float)) AS TotalLoss ... ?
13. I don't care if its pretty, I just need it faster.
14. How can I get the summing of T_INCIDENT_LST.TRANS_AMNT without a
derived table...? I know that using the derived table is slowing it
down some.
**** Any Ideas ****
David
View 3 Replies
ADVERTISEMENT
Mar 4, 2008
Hello,
We have some queries that are long and intensive. We have thought about running the queries and storing the data in a text file for lookup from our website.
Example: Our online store only displays items that are in stock so when a user selects a category a query runs and grabs only items that are in stock and then displays them. There could be thousands of items the query needs to sort through before displaying the items that are in stock. What if we ran this query once every hours an stored the results in a txt file? The asp page would then go to the text file to grab the results instead of having to run the query every time a user selects a category. Will this speed up the site by not having to query every time? Would this be a correct way to eliminate queries that run thousands of times a day?
thanks
Andy
View 2 Replies
View Related
Jul 23, 2005
Hello everybody,Just short question:I have tables, which are only log tables (very less used for selects),but there is a lotof writing.I would like to have as much speed as possible by writing data intothis tables.create table [tbl] ([IDX] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,[Time_Stamp] [datetime] NOT NULL ,[Source] [varchar] (64) COLLATE Latin1_General_CI_AS NULL ,[Type] [varchar] (16) COLLATE Latin1_General_CI_AS NULL ,[MsgText] [varchar] (512) COLLATE Latin1_General_CI_AS NULL ,CONSTRAINT [tbl] PRIMARY KEY NONCLUSTERED([IDX]) ON [PRIMARY]) ON [PRIMARY]GOQuestion:Is it better for inserts,, to remove PK but leave identity insert?How to make this table optimized for writing?If I will set fill level of the table with 0%, will I winn much?Once information: this table will be deleted with old data, dependingon row count (oldest ID's will be deleted each night).Thank You in advanceMateusz
View 2 Replies
View Related
Mar 29, 2006
according to the mysql manual, multiple inserts can be sped up bylocking the table before doing them and unlocking the table afterwards.is the same true of multiple inserts in mysql? if so, how would thetable be locked?any insights would be appreciated - thanks!
View 3 Replies
View Related
Jul 20, 2005
We have a dynamic SP that dependant on a user name will run a selectedtailored to them.One of the criteria is the number of rows retrieved, which we include using'top @varNoOfRows' in the dynamically created select .As long as the number of rows is less than 130 the SP runs in less than asecond, but if you increase that value to over 150 the SP times out.It is being run from ASP in this way: DBCon.execute(SQLQuery)The main table that we are querying contains about 1.5 million records andis un-indexed. (eek - didn't realise that until I just checked) on SQLserver 2000.Does anyone have any pointers towards streamlining the SP - I can post it ifyou wish. Or can anyone explain how to use the execute plan to ouradvantage?I've already used it to change aSET @statement2 = (select T1_QueryPart from Table1 where T1_ID like (SELECTLoginTable_T1ID from LoginTable where @username = LT_UserName))toSET @T1ID = (SELECT LT_T1ID from LoginTable where @username = LT_UserName)SET @statement2 = (select T1_QueryPart from Table1 where T1_ID like @T1ID)But would , say, a join be more time efficient?Any help would be appreciatedJohn
View 3 Replies
View Related
Apr 14, 2008
I have a forum topic, that has comments. On the homepage, a widget shows the most recent 6 comments across all topics. Some of these topics have 7000+ comments. On the actual topic page, the comments are paged, 10 records per page. In the widget, if the user clicks on the comment, it should take them directly to the comment, and the page it is on. (The most recent comment is on the last page). So, to link it would be e.g. linktoforumtopic.aspx?p=177#commentID=999To get the page number the comment is on, I would have to return all the comments(7000+), get the rowindex of the comment, and figure out what page it is on depending on the page size. This all works, however it is extremely slow. Can't think of a better way....DECLARE @RowIndex decimal DECLARE @PageIndex int SET @RowIndex = (SELECT [RowIndex] FROM @Results WHERE CommonID = @BlogCommentID) SET @PageIndex = 1 IF(@RowIndex > 10) BEGIN SET @PageIndex = CEILING(@RowIndex / @PageSize) END SELECT @PageIndex AS PageIndex
View 1 Replies
View Related
Dec 19, 2000
Hi all...
I have a table with over 60 million rows (approx 20GB) which has an indexed column. I have tried using DBC DBReindex to rebuild the index, but after kicking it off on a friday, it is still running the following wednesday. Since managers and other finicky types access this database, that's not acceptable (it slows down their reporting).
Is there a way to speed up the reindexing process? Perhaps by adding space to the tempdb (it's 500MB) or putting it in RAM temporarily? I haven't seen any articles that specifically state that TEMPDB is used during an index rebuild, but it seems logical that it would be.
Any suggestions to speed up the process would be most appreciated!
View 2 Replies
View Related
May 26, 2004
Hello, can anyone offer any advice on this problem related to store procedures.
The following 2 chunks of SQL illustrate the problem
--1
declare @lsFilt varchar(16)
select @lsFilt = 'fil%ter'
select * from sysobjects where name like @lsFilt
--2
declare @lsQuery varchar(128)
select @lsQuery = 'select * from sysobjects where name like ''fil%ter'''
exec (@lsQuery)
When I view the execution plan the cost % breakdown is approx 82%, 18%. The second query does a bookmark lookup and an index seek while the first slow query does a clustered index seek and takes approx 5 times longer to do.
Now my real question is suppose I have an store procedure to run a similar query. Should be writing my SPs along the lines of
create proc SP2Style
@psFilter varchar(16)
AS
declare @lsQuery varchar(128)
select @lsQuery = 'select * from sysobjects where name like ''' @psFilter + ''''
exec (@lsQuery)
GO
instead of
create proc SP1Style
@psFilter varchar(16)
AS
select * from sysobjects where name like @psFilter
GO
Is there another way to write similar store procedures without using dynamic query building or the exec but keep the faster execution speed?
thanks
Paul
View 2 Replies
View Related
Feb 23, 2004
http://www.aspalliance.com/349
View 1 Replies
View Related
Apr 21, 2015
SELECT
CONVERT(VARCHAR(10),attnc_chkin_dt,101) as INDATE,
CONVERT(VARCHAR(10),attnc_chkin_dt,108) as TimePart
FROM pmt_attendance
o/p
indate 04/18/2015
time part :17:45:00
I need to convert this 17:45:00 to 12 hours date format...
View 8 Replies
View Related
May 26, 2005
Hi,
I have a table which has a few fields, one being "datetime_traded". I need to write a query which returns the row which has the closest time (down to second) given a date/time. I'm using MS SQL.
Here's what I have so far:
Code:
select * from TICK_D
where datetime_traded = (select min( abs(datediff(second,datetime_traded , Convert(datetime,'2005-05-30:09:31:09')) ) ) from TICK_D)
But I get an error - "The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.".
Does anyone know how i could do this? Thanks a lot for any help!
View 2 Replies
View Related
May 15, 2008
Hello All,
Below carry takes too much time while execution
Select
'PIT_ID' = CASE WHEN Best_BID_DATA.PIT_ID IS NOT NULL THEN Best_BID_DATA.PIT_ID ELSE Best_OFFER_DATA.PIT_ID END,
Best_Bid_Data.Bid_Customer,
Best_Bid_Data.Bid_Size,
Best_Bid_Data.Bid_Price,
Best_Bid_Data.Bid_Order_Id,
Best_Bid_Data.Bid_Order_Version,
Best_Bid_Data.Bid_ProductId,
Best_Bid_Data.Bid_TraderId,
Best_Bid_Data.Bid_BrokerId,
Best_Bid_Data.Bid_Reference,
Best_Bid_Data.Bid_Indicative,
Best_Bid_Data.Bid_Park,
Best_Offer_Data.Offer_Customer,
Best_Offer_Data.Offer_Size,
Best_Offer_Data.Offer_Price,
Best_Offer_Data.Offer_Order_Id,
Best_Offer_Data.Offer_Order_Version,
Best_Offer_Data.Offer_ProductId,
Best_Offer_Data.Offer_TraderId,
Best_Offer_Data.Offer_BrokerId,
Best_Offer_Data.Offer_Reference,
Best_Offer_Data.Offer_Indicative,
Best_Offer_Data.Offer_Park
from
(
Select PITID PIT_ID, CustomerId Bid_Customer, Size Bid_Size, Price Bid_Price, orderid Bid_Order_Id, Version Bid_Order_Version,
ProductId Bid_ProductId, TraderId Bid_TraderId, BrokerId Bid_BrokerId,
Reference Bid_Reference, Indicative Bid_Indicative, Park Bid_Park
From OrderTable C
Where
version = (select max(version) from OrderTable where orderid = c.orderid)
and BuySell = 'B'
and Status <> 'D'
and Park <> 1
and PitId in (select distinct pitid from MarketViewDef Where MktViewId = 4)
and Price =
( Select max(Price) From OrderTable cc
where version = (select max(version) from OrderTable where orderid = cc.orderid)
and PitId = c.PitId
and BuySell = 'B'
and Status <> 'D'
and Park <> 1
)
and Orderdate =
( Select min(Orderdate) From OrderTable dd
where version = (select max(version) from OrderTable where orderid = dd.orderid)
and PitId = c.PitId
and BuySell = 'B'
and Status <> 'D'
and Price = c.Price
and Park <> 1
)
and OrderId = (select top 1 OrderId from OrderTable ff
Where version = (select max(version) from OrderTable where orderid = ff.orderid)
and orderid = ff.orderid
and PitId = c.PitId
and BuySell = 'B'
and Status <> 'D'
and Price = c.Price
and Orderdate = c.Orderdate
and Park <> 1
)
) Best_Bid_Data
full outer join
(
Select PITID PIT_ID, CustomerId Offer_Customer, Size Offer_Size, Price Offer_Price, orderid Offer_Order_Id, Version Offer_Order_Version,
ProductId Offer_ProductId, TraderId Offer_TraderId, BrokerId Offer_BrokerId,
Reference Offer_Reference, Indicative Offer_Indicative, Park Offer_Park
From OrderTable C
Where
version = (select max(version) from OrderTable where orderid = c.orderid)
and BuySell = 'S'
and Status <> 'D'
and Park <> 1
and PitId in (select distinct pitid from MarketViewDef Where MktViewId = 4)
and Price =
( Select min(Price) From OrderTable cc
where version = (select max(version) from OrderTable where orderid = cc.orderid)
and PitId = c.PitId
and BuySell = 'S'
and Status <> 'D'
and Park <> 1
)
and Orderdate =
( Select min(Orderdate) From OrderTable dd
where version = (select max(version) from OrderTable where orderid = dd.orderid)
and PitId = c.PitId
and BuySell = 'S'
and Status <> 'D'
and Price = c.Price
and Park <> 1
)
and OrderId = (select top 1 OrderId from OrderTable ff
Where version = (select max(version) from OrderTable where orderid = ff.orderid)
and orderid = ff.orderid
and PitId = c.PitId
and BuySell = 'S'
and Status <> 'D'
and Price = c.Price
and Orderdate = c.Orderdate
and Park <> 1
)
) Best_Offer_Data
ON Best_Bid_Data.Pit_Id = Best_Offer_Data.Pit_Id
Can any one please help me?
Thanks
Prashant
View 2 Replies
View Related
Aug 7, 2007
Hi all,
I have created a report in SSRS 2005 which is being viewed by users from different Time Zones.
I have a dataset which has a field of type datetime (UTC). Now I would like to display this Date according to the User Time Zone.
For example if the date is August 07, 2007 10:00 AM UTC,
then I would like to display it as August 07, 2007 03:30 PM IST if the user Time Zone is IST.
Similarly for other Time Zones it should display the time accordingly.
Is this possible in SSRS 2005?
Any pointers will be usefull...
Thanks in advance
sudheer racha.
View 5 Replies
View Related
Nov 14, 2007
My asp.net application is attached with SQL database, I record only time in my SQL database , field type is nvarchar. Now I want to do qurey by time and pull the result , but datatype of field in nvarchar, query does not giving me right result.
some one tell me how Do I do query so I can get proper result.
thanks maxmax
View 3 Replies
View Related
Nov 16, 2007
I have asp.net applicatin with SQL database communicating. in database I have date field and time field. Now I wan to do SQL query which can pull informatin on particular date between given start time to given end time
Can some one show me sample SQL query so I can pull informatin on particular day between two times
thank you
maxmax
View 2 Replies
View Related
Feb 4, 2008
How do I find all the records within a time range when all the datetime's are on different days?
Example: Show me all the order placed between 12:00 and 15:00 in May
View 1 Replies
View Related
Nov 21, 2007
hi,
how can i define a simple time-out exceed when running a simple select query like
select a from atable where a > 1
and define if query doesn't give results in 5 sec, it should be stopped.
View 2 Replies
View Related
Apr 8, 2008
I need to time a query down to hundredths of a second. I can see from the runtime clock in SQL Studio that my query ran for 00:00:00. However, I need to know exactly how long it took.
Thanks for your help!
View 4 Replies
View Related
Oct 5, 2007
Hello,I have a table that lists a number of available time slots for a party venue. I want a user to select a particular time slot and have the query show results for that time slot plus two time slots before and two time slots after the user's selection. If the user selects a time slot that is the first of the day, I still want that user to be shown 5 results including the one he selected. The same goes for the last time slot of the day. Can anyone help me with the SQL statement?
Thank you very much in advance!
Mark
View 12 Replies
View Related
Feb 18, 2004
I am using SQL Server and ASP.NET. I am executing a couple of stored procedures and displaying the results in a datagrid. Since these Stored procedures takes around 2-4 minutes each, I want to display a status bar on the web by displaying the approximate time the user needs to wait before seeing the results.
My question is: Is there a way to find out the approximate EXECUTION TIME of the stored procedure before hand. Also, if that is possible, how do i access the same from the ASP.NET code..
Thanks
Sathya
View 2 Replies
View Related
Apr 27, 2004
Hello,
I have a table which has a field called time and it has data like '23/04/2004 9:43:40 AM'
How would i write a query that would retrieve the last hour of data from now().
i've tried this but does not work,
SELECT [time]
FROM table
WHERE ([time] = { fn NOW() } - 60)
Thanks
Goong
View 3 Replies
View Related
Mar 14, 2002
I have a server with two test instances of a data base. I have a query which creates a temp table, inserts 29 rows, perform 4 update queries to add counts and then dumpps out the results. This entire query script runs 1.33 minutes on one instance and 2.5 minutes on the other. On the production server this query now runs in 9 seconds. If I run any one of the test updates individually they execute under 2 seconds, just like the production server.
THe results are repeatable.
All are SQL 7 with all service packs on NT4 sp6. Both test data bases are backups of production from last week. I suspect some kind of caching/buffer problem, but I do not know what to look for. I am not a DBA so I have no idea what role TEMPDB plays may play in this.
Can anyone give us ideas on where to look for the performance difference? Will our impending upgrade to SQL2K solve this problem or make it worse? Any ideas would be appreciated.
View 1 Replies
View Related
Mar 30, 2001
Why is the response time to run a query faster when the output is displayed in grid format as compared to text format?
View 1 Replies
View Related
Jun 5, 2001
Hi, We have a SQL 7 / Win2K cluster and yesterday afternoon the users were complaining about poor performance. Their queries were timing out.. (Not all of them, just some on some large tables)
I ran just an ad-hoc query against the table from my machine and I also timed out. THen I went right to the box that had control of the cluster and did the same thing there and also timed out. Because of time constraints (and we are in testing mode) we tested a failover and everything was back to normal after that.
So now we want to try to figure out what could have been the problem. At the time I checked out the Memory and CPU usage and they were very low (0-5%) and using only 1/3 of the memory. It couldn't be a bad query or index because after the failover it worked fine.
Could there be something wrong with the specific box that had control at the time? I dont' know where to look?
Any ideas would be appreciated!
View 1 Replies
View Related
Sep 13, 2001
Hi,
I am running this query and it is taking over 3 minutes.
"select * from table1 where CONVERT(varchar(10),dated,5) = '13-09-01' "
Table1 has a column called dated which is datetime datatype.
Any suggestions how can i optimize this query?I tried Non-clustered index on Dated column and time came down to less than 3 but still more than 2min.
TIA.
View 4 Replies
View Related
Aug 1, 2003
Is there a way in sql server to find out how much time a sql server took to execute a query.
Thanx in advance.
Regards,
Samir
View 2 Replies
View Related
Aug 19, 2002
This is probably a simple question, but I am relatively new using SQL Queries.
I tried this which gives me half of what I need... SET STATISTICS IO ON
I also need my query to return the run time that it took to run my query.
Can someone help me please?
Thanks!
David
View 2 Replies
View Related
Jul 7, 2006
Aside from indexes, will it help if I use multiple filegroups to improve the time needed to query millions of records?
View 2 Replies
View Related
Jun 11, 2006
I've been baffled over how to do this without using a script... I would like to get the info I need with a single query.
Here's my scenario, the table looks like this (simplified):
ID, TIME
101, 5am
101, 6am
104, 5am
260, 5am
104, 6am
260, 6am
101, 7am
260, 9am
104, 7am
101, 8am
So basically I have a column of identifications and a column of times. They won't necessarily be in order. I would like a query that gives me this:
101, 8am
104, 7am
260, 9am
It would order the IDs ascending, only showing the newest time assigned to that ID in the table.
Thanks in advance for any help :)
View 14 Replies
View Related
Jul 9, 2007
Hey guys and gals,
I'm having a real problem with this query at the moment...
Basically I have to produce a query which will tell me the total number of people employed by the company at any given date and the total salary for all these people.
We have a people table and a career table.
People(unique_identifier, known_as_and_surname, start_date, termination_date ...)
Career(unique_identifier, parent_identifier, career_date, basic_pay ...)
Relationship people.unique_identifier = career.parent_identifier
Employees can be identified like so
SELECT *
FROM people
WHERE start_date <= DateSelected
AND (termination_date > DateSelected
OR termination_date IS NULL)
Passing the selected date to the query is no trouble at all I am just having problems with the point in time side of this.
All and any help is greatly appreciated :)
~George
P.S. SQL Server 2000 ;)
View 14 Replies
View Related
May 20, 2004
We have a query (a few actually) which runs for about 30 secs via Siebel 6 and the same query takes on 1 or 2 secs in Query Analyser, consistently. Naturally this performance problem is causing issues. We're wondering if the App isn't using the same execution plan, or using the indexes or....
It might be worth noting that the query returns 1 or no rows.
(I can add the query and more detail if anyone really, really wants :rolleyes: )
The devlopers have created a VB app which mimmics to app running the query and we've put it through proflier, results..
Duration Reads CPU
Siebel Query 28300 3661280 23984
It seems a high number of reads there, and the result from QA is SO much faster.
Any ideas welcome, thanks.
View 5 Replies
View Related
May 13, 2008
i want to query all my records and how many per date
so
select count(id),date from registrants group by date
but my problem is the date is a date time field so how can i just group it by date but not count the time?
View 2 Replies
View Related
Jun 6, 2008
My table like as follow
Results
Date | Time | Value
---------------------------
4/4/2007 | 0 | 879
4/4/2007 | 5 | 600
4/4/2007 | 10 | 390
4/4/2007 | 15 | 490
4/4/2007 | 20 | 290
...
...
...
4/4/2007 | 100 | 290
4/4/2007 | 105 | 290
4/4/2007 | 110 | 290
...
...
4/4/2007 | 1210 | 290
4/4/2007 | 1215 | 290
Date is DateTime, Time is Integer and Value is Integer
If Time=0, mean 1200 AM
If Time=5, mean 1205 AM
If Time=1210, mean 1210 PM
Let's say Current Date=4 April 2007 and Current Time=1206 AM
I want to display data which is Date>=4 April 2007 and Time>=1206 AM
How to query to get expected result shown as follow
Date | Time | Value
---------------------------
4/4/2007 | 10 | 390
4/4/2007 | 15 | 490
4/4/2007 | 20 | 290
...
...
...
4/4/2007 | 100 | 290
4/4/2007 | 105 | 290
4/4/2007 | 110 | 290
...
...
4/4/2007 | 1210 | 290
4/4/2007 | 1215 | 290
View 12 Replies
View Related