473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Slow Sql Query

12
Hi there, I'm new to this forum, but I've come here quite a bit to find solutions to problems other people have encountered... I'm currently trying to query a database up to 5000 times as fast as possible to retrieve certain pricing data for a list of companies. I've run into a few queries that seem to be taking much longer then others and ultimately slow down my function call to a point where it is no longer useful. For instance... I have 2 different catalogues for different sources of data... one holds information regarding pricing, the other holds information regarding the trading volume.... the pricing query executes for all 5000 companies in less then 10 seconds, but the trading volume takes upwards of 5 minutes. There are 2 queries for each catalogue that seem to be running slow... I've tried to eliminate computational items from the queries to speed things up, but this still does not help. Here are 2 of the slow queries(as stored procedures):

Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE dbo.GetQuarterlyEPS
  2.     (
  3.     @Gvkey int,
  4.     @StartDate smalldatetime,
  5.     @EndDate smalldatetime
  6.     )
  7. AS
  8.     SELECT     gvkey, datadate, epspxq
  9.     FROM       co_ifndq
  10.     WHERE     (gvkey = @Gvkey) AND consol = 'C' AND datadate <= @StartDate AND datadate > @EndDate
  11.     ORDER BY datadate DESC
  12.     RETURN
  13.  
  14. ALTER PROCEDURE dbo.GetMeanEstimate
  15.     (
  16.     @ibtic nchar(3),
  17.     @StartFirstDate smalldatetime,
  18.     @EndFirstDate smalldatetime,
  19.     @StartSecondDate smalldatetime,
  20.     @EndSecondDate smalldatetime
  21.     )
  22. AS
  23.  
  24.     SELECT IBESStatisticalPeriod, MeanEstimate 
  25.     FROM USSummaryStatisticsAdjusted
  26.     WHERE IBESTicker = @ibtic 
  27.     AND 
  28.         (
  29.             (IBESStatisticalPeriod <= @StartFirstDate
  30.             AND IBESStatisticalPeriod > @EndFirstDate) 
  31.         OR 
  32.             (IBESStatisticalPeriod <= @StartSecondDate 
  33.             AND IBESStatisticalPeriod > @EndSecondDate)
  34.         ) 
  35.     AND ForecastPeriodIndicator = 1
  36.     ORDER BY IBESStatisticalPeriod DESC
  37.     RETURN
  38.  
  39.  
I'm not an SQL guy... my forte is much more C++/C#, so please keep that in mind if you have a solution or any ideas to help speed things up.

The tables have upwards of 100,000 entries each and in some cases more then 300 columns. Would doing a subquery for the columns I need and then filtering the results speed things up?

Thanks! - Will
Feb 1 '08 #1
4 2668
ck9663
2,878 Expert 2GB
Hi there, I'm new to this forum, but I've come here quite a bit to find solutions to problems other people have encountered... I'm currently trying to query a database up to 5000 times as fast as possible to retrieve certain pricing data for a list of companies. I've run into a few queries that seem to be taking much longer then others and ultimately slow down my function call to a point where it is no longer useful. For instance... I have 2 different catalogues for different sources of data... one holds information regarding pricing, the other holds information regarding the trading volume.... the pricing query executes for all 5000 companies in less then 10 seconds, but the trading volume takes upwards of 5 minutes. There are 2 queries for each catalogue that seem to be running slow... I've tried to eliminate computational items from the queries to speed things up, but this still does not help. Here are 2 of the slow queries(as stored procedures):

Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE dbo.GetQuarterlyEPS
  2.     (
  3.     @Gvkey int,
  4.     @StartDate smalldatetime,
  5.     @EndDate smalldatetime
  6.     )
  7. AS
  8.     SELECT     gvkey, datadate, epspxq
  9.     FROM       co_ifndq
  10.     WHERE     (gvkey = @Gvkey) AND consol = 'C' AND datadate <= @StartDate AND datadate > @EndDate
  11.     ORDER BY datadate DESC
  12.     RETURN
  13.  
  14. ALTER PROCEDURE dbo.GetMeanEstimate
  15.     (
  16.     @ibtic nchar(3),
  17.     @StartFirstDate smalldatetime,
  18.     @EndFirstDate smalldatetime,
  19.     @StartSecondDate smalldatetime,
  20.     @EndSecondDate smalldatetime
  21.     )
  22. AS
  23.  
  24.     SELECT IBESStatisticalPeriod, MeanEstimate 
  25.     FROM USSummaryStatisticsAdjusted
  26.     WHERE IBESTicker = @ibtic 
  27.     AND 
  28.         (
  29.             (IBESStatisticalPeriod <= @StartFirstDate
  30.             AND IBESStatisticalPeriod > @EndFirstDate) 
  31.         OR 
  32.             (IBESStatisticalPeriod <= @StartSecondDate 
  33.             AND IBESStatisticalPeriod > @EndSecondDate)
  34.         ) 
  35.     AND ForecastPeriodIndicator = 1
  36.     ORDER BY IBESStatisticalPeriod DESC
  37.     RETURN
  38.  
  39.  
I'm not an SQL guy... my forte is much more C++/C#, so please keep that in mind if you have a solution or any ideas to help speed things up.

The tables have upwards of 100,000 entries each and in some cases more then 300 columns. Would doing a subquery for the columns I need and then filtering the results speed things up?

Thanks! - Will
am not much of a sql optimizer...but i would assume am the one creating your application i would do the following:
1. create index with those searched columns as keys. if your table has only 1 index, it's recommended to be clustered. it does not have to unique.
2. use BETWEEN to check those dates
3. conditions are processed from left to right. in an AND condition, only one has to be false to make the entire condition false. so re-arrange your conditions properly based on what your data would most probably show.

-- CK
Feb 1 '08 #2
Delerna
1,134 Expert 1GB
Agree with all that CK says.
Indexes will have the biggest impact on speeding up your queries.
I have seen indexes take 10 minute queries down to seconds

Looking at your queries those 2 date fields look like good candidates
but think carefully about the fields you choose to index.
Indexes speed reads of your data but they also slowdown writes.
This is because each time a record is written to, any an all indexs
also have to be updated. If you have too many indexes all your reads
will be superfast but the writes can become unuseable.
Feb 1 '08 #3
bfoo75
12
Thanks for the response guys... I've managed to speed up 2 of my 4 slow queries with the keys for one of the databases, but the queries that access the tables with 300+ columns are still performing very slow. They're already keyed with 6 different columns and I'm querying using 3 in the where clauses. I'm assuming I shouldn't index them if they are already keyed.

Here are the remaining Queries:

Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE dbo.Get5YearQuarterlyEPS
  2.     (
  3.     @Gvkey int,
  4.     @Date smalldatetime,
  5.     @EndDate smalldatetime
  6.     )
  7. AS
  8.     SELECT     gvkey, datadate, epspxq
  9.     FROM       co_ifndq
  10.     WHERE     (gvkey = @Gvkey) AND (datadate BETWEEN @Date AND @EndDate) AND consol = 'C' 
  11.     ORDER BY datadate DESC
  12.     RETURN
  13.  
  14.  
  15. ALTER PROCEDURE dbo.GetQuarterlyEPS
  16.     (
  17.     @Gvkey int,
  18.     @StartDate smalldatetime,
  19.     @EndDate smalldatetime
  20.     )
  21. AS
  22.     SELECT     gvkey, datadate, epspxq
  23.     FROM       co_ifndq
  24.     WHERE     (gvkey = @Gvkey) AND datadate BETWEEN @StartDate AND @EndDate AND consol = 'C' 
  25.     ORDER BY datadate DESC
  26.     RETURN
  27.  
Without these queries, 1 iteration of data collection takes about 3-4 seconds... with them, it takes about 3 minutes. If I change the order by to match the same as the indecies, would that possibly speed things up?
Feb 4 '08 #4
bfoo75
12
Ahhhh never mind. Problem Solved. I created indecies as well as keys and everything spead up. Thanks so much for the help, the suggestions were excactly what I needed. Cheers - Will
Feb 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Shay | last post by:
essentially I am trying to do some counts based on some assumptions in the recordset. So I get the RS back, put the values into a variable, move to the next record in the RS and compare what is in...
2
by: Tim Fountain | last post by:
We've recently enabled slow query logging on a server and it's proving interesting seeing which queries are bogging things down. This one is puzzling me a little: SELECT articleid, type,...
1
by: Thomas Bartkus | last post by:
Is it possible that the elapsed time being measured includes waiting for the client to acknowledge that it has received all the data? In *addition* to the server execution time? Documentation...
2
by: AG | last post by:
I am able to run a query which runs FAst in QA but slow in the application.It takes about 16 m in QA but 1000 ms on the Application.What I wanted to know is why would the query take a long time...
2
by: Yonatan Goraly | last post by:
I am in the process of adding PostgreSQL support for an application, in addition to Oracle and MS SQL. I am using PostgreSQL version 7.3.2, Red Hat 9.0 on Intel Pentium III board. I have a...
3
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. ...
3
by: Jennyfer J Barco | last post by:
In my application I have a datagrid. The code calls a Stored procedure and brings like 200 records. I created a dataset and then a dataview to bind the results of the query to my grid using ...
0
by: Dave Hammond | last post by:
Hi All, I'm trying to use the slow-query-log (with --log-queries-not-using-indexes enabled) to determine if any queries need optimization, and have a few questions about some entries I'm...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.