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): -
ALTER PROCEDURE dbo.GetQuarterlyEPS
-
(
-
@Gvkey int,
-
@StartDate smalldatetime,
-
@EndDate smalldatetime
-
)
-
AS
-
SELECT gvkey, datadate, epspxq
-
FROM co_ifndq
-
WHERE (gvkey = @Gvkey) AND consol = 'C' AND datadate <= @StartDate AND datadate > @EndDate
-
ORDER BY datadate DESC
-
RETURN
-
-
ALTER PROCEDURE dbo.GetMeanEstimate
-
(
-
@ibtic nchar(3),
-
@StartFirstDate smalldatetime,
-
@EndFirstDate smalldatetime,
-
@StartSecondDate smalldatetime,
-
@EndSecondDate smalldatetime
-
)
-
AS
-
-
SELECT IBESStatisticalPeriod, MeanEstimate
-
FROM USSummaryStatisticsAdjusted
-
WHERE IBESTicker = @ibtic
-
AND
-
(
-
(IBESStatisticalPeriod <= @StartFirstDate
-
AND IBESStatisticalPeriod > @EndFirstDate)
-
OR
-
(IBESStatisticalPeriod <= @StartSecondDate
-
AND IBESStatisticalPeriod > @EndSecondDate)
-
)
-
AND ForecastPeriodIndicator = 1
-
ORDER BY IBESStatisticalPeriod DESC
-
RETURN
-
-
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
4 2668
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): -
ALTER PROCEDURE dbo.GetQuarterlyEPS
-
(
-
@Gvkey int,
-
@StartDate smalldatetime,
-
@EndDate smalldatetime
-
)
-
AS
-
SELECT gvkey, datadate, epspxq
-
FROM co_ifndq
-
WHERE (gvkey = @Gvkey) AND consol = 'C' AND datadate <= @StartDate AND datadate > @EndDate
-
ORDER BY datadate DESC
-
RETURN
-
-
ALTER PROCEDURE dbo.GetMeanEstimate
-
(
-
@ibtic nchar(3),
-
@StartFirstDate smalldatetime,
-
@EndFirstDate smalldatetime,
-
@StartSecondDate smalldatetime,
-
@EndSecondDate smalldatetime
-
)
-
AS
-
-
SELECT IBESStatisticalPeriod, MeanEstimate
-
FROM USSummaryStatisticsAdjusted
-
WHERE IBESTicker = @ibtic
-
AND
-
(
-
(IBESStatisticalPeriod <= @StartFirstDate
-
AND IBESStatisticalPeriod > @EndFirstDate)
-
OR
-
(IBESStatisticalPeriod <= @StartSecondDate
-
AND IBESStatisticalPeriod > @EndSecondDate)
-
)
-
AND ForecastPeriodIndicator = 1
-
ORDER BY IBESStatisticalPeriod DESC
-
RETURN
-
-
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
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.
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: -
ALTER PROCEDURE dbo.Get5YearQuarterlyEPS
-
(
-
@Gvkey int,
-
@Date smalldatetime,
-
@EndDate smalldatetime
-
)
-
AS
-
SELECT gvkey, datadate, epspxq
-
FROM co_ifndq
-
WHERE (gvkey = @Gvkey) AND (datadate BETWEEN @Date AND @EndDate) AND consol = 'C'
-
ORDER BY datadate DESC
-
RETURN
-
-
-
ALTER PROCEDURE dbo.GetQuarterlyEPS
-
(
-
@Gvkey int,
-
@StartDate smalldatetime,
-
@EndDate smalldatetime
-
)
-
AS
-
SELECT gvkey, datadate, epspxq
-
FROM co_ifndq
-
WHERE (gvkey = @Gvkey) AND datadate BETWEEN @StartDate AND @EndDate AND consol = 'C'
-
ORDER BY datadate DESC
-
RETURN
-
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?
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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. ...
|
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 ...
|
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...
|
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
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |