473,396 Members | 1,995 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,396 software developers and data experts.

slow query thousands of records.

I have a query that returns raw tick data from a table. Unfortunately after
even a few days there are hundreds of thousands of rows so the following
query is not efficient.

SELECT * FROM RAWTICKDATA WHERE Status = 'I' AND ContractCode = ? AND
RawTickID = (SELECT Max(RawTickID) FROM RAWTICKDATA WHERE Status = 'I' AND
ContractCode = ? AND PRICE =(SELECT Min(Price) FROM RAWTICKDATA WHERE Status
= 'I' AND ContractCode = ?))

The most obvious solution then is to get all tick data with status ='I'
(Imported) for a contract, process it and then move it to another table for
archiving. I am faced with a problem however: After selecting all data for a
contract with status='I' the application was updating these records to a new
status of 'P' (processed). Unfortunately another application is continuning
to feed in live data and so it is possible that we will inadvertantly update
unprocessed data to 'P'.

Question: Is it possible to select all records with status 'I' (from above
query) and update their status to 'P' in one sequence?

I am not a programmer, but if this is possible I should be able to implement
the query.

Many thanks.
Steve
Jul 20 '05 #1
3 2207
Steve (s@h.com) writes:
I have a query that returns raw tick data from a table. Unfortunately
after even a few days there are hundreds of thousands of rows so the
following query is not efficient.

SELECT * FROM RAWTICKDATA WHERE Status = 'I' AND ContractCode = ? AND
RawTickID = (SELECT Max(RawTickID) FROM RAWTICKDATA WHERE Status = 'I' AND
ContractCode = ? AND PRICE =(SELECT Min(Price) FROM RAWTICKDATA WHERE
Status = 'I' AND ContractCode = ?))

The most obvious solution then is to get all tick data with status ='I'
(Imported) for a contract, process it and then move it to another table
for archiving. I am faced with a problem however: After selecting all
data for a contract with status='I' the application was updating these
records to a new status of 'P' (processed). Unfortunately another
application is continuning to feed in live data and so it is possible
that we will inadvertantly update unprocessed data to 'P'.


Before trying to rearrange the data flow, it could be worth to see if
you can speed up the query.

I assumed that RawTickID is the primary key of the table. For this
query, it would probably be best to have the clustered index on
ConstractCode. (But there may be other queries which this is not good.)

Here is also rewrite which you can try together with a clustered index on
ContractCode:

SELECT *
FROM RAWTICKDATA a
JOIN (SELECT maxid = MAX(b.RawTickId)
FROM RAWTICKDATA b
JOIN (SELECT price = MIN(c.Price)
FROM RAWTICKDATA c
WHERE c.Status = 'I'
AND c.ContractCode = ?) AS d
ON b.price = d.price
WHERE c.Status = 'I'
AND c.ContractCode = ?) AS e
ON a.RawTickId = e.maxid
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Hi Erland,

I tried your query but it gives me the following error. "The column prefix
"c" does not match with a table name or alias name used in the query.

Just out of interest, how many rows can be stored in sql2000. Is it a good
medium for storage of tickdata or am I better off archiving to something
else?

Steve
"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn**********************@127.0.0.1...
Steve (s@h.com) writes:
I have a query that returns raw tick data from a table. Unfortunately
after even a few days there are hundreds of thousands of rows so the
following query is not efficient.

SELECT * FROM RAWTICKDATA WHERE Status = 'I' AND ContractCode = ? AND
RawTickID = (SELECT Max(RawTickID) FROM RAWTICKDATA WHERE Status = 'I' AND ContractCode = ? AND PRICE =(SELECT Min(Price) FROM RAWTICKDATA WHERE
Status = 'I' AND ContractCode = ?))

The most obvious solution then is to get all tick data with status ='I'
(Imported) for a contract, process it and then move it to another table
for archiving. I am faced with a problem however: After selecting all
data for a contract with status='I' the application was updating these
records to a new status of 'P' (processed). Unfortunately another
application is continuning to feed in live data and so it is possible
that we will inadvertantly update unprocessed data to 'P'.


Before trying to rearrange the data flow, it could be worth to see if
you can speed up the query.

I assumed that RawTickID is the primary key of the table. For this
query, it would probably be best to have the clustered index on
ConstractCode. (But there may be other queries which this is not good.)

Here is also rewrite which you can try together with a clustered index on
ContractCode:

SELECT *
FROM RAWTICKDATA a
JOIN (SELECT maxid = MAX(b.RawTickId)
FROM RAWTICKDATA b
JOIN (SELECT price = MIN(c.Price)
FROM RAWTICKDATA c
WHERE c.Status = 'I'
AND c.ContractCode = ?) AS d
ON b.price = d.price
WHERE c.Status = 'I'
AND c.ContractCode = ?) AS e
ON a.RawTickId = e.maxid
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #3
Steve (s@h.com) writes:
I tried your query but it gives me the following error. "The column prefix
"c" does not match with a table name or alias name used in the query.
The usual recommendation is to provide CREATE TABLE statements together
with sample data as INSERT statements. That makes it possible to post
a tested solution.

When this is missing, it is common to post just an example, and the
person who is having the problem is assumed to be able to cope with
simple syntax errors. Yeah, I know you said that you are not a programmer,
but there is some basic idea in these newsgroups that you get help to
help yourself. After all, suggestions here need to be tested and integrated.

For this case, try replacing the offending c with b.
Just out of interest, how many rows can be stored in sql2000. Is it a good
medium for storage of tickdata or am I better off archiving to something
else?


SQL Server is an enterprise DBMS, so you would have to get a really huge
amount of data to push the limits. The maximum size for a database is
1,048,516 TB (except in MSDE where it is 2GB.) There is no particular max
number for rows in a table.

In Books Online, SQL Server Architecture, Implementation Details there
is a topic on maximum specifications.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DJJ | last post by:
I am using the MySQL ODBC 3.51 driver to link three relatively small MySQL tables to a Microsoft Access 2003 database. I am finding that the data from the MySQL tables takes a hell of a long time...
3
by: Bob C. | last post by:
When I migrated my tables to SQL Server I needed a way to overcome the slow performance of the Find method on my recordsets. Although this can be done by accessing the table directly using dao and...
1
by: Coy Howe | last post by:
This one seems bizarre! We have a database consisting of a main table and 12 - 15 "sub" tables, which are connected via cascading relationships. The database performs many complex calculations...
5
by: Maxi | last post by:
I have 162 tables in my database. Names of the Tables are 1, 2, ...... so on till 162. Every table has only one field (field name = Expr2) of type NUMBER (DOUBLE) with 352716 records in each table....
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 ...
29
by: Geoff Jones | last post by:
Hi All I hope you'll forgive me for posting this here (I've also posted to ado site but with no response so far) as I'm urgently after a solution. Can anybody help me? I'm updating a table on...
3
by: Kai Zhang | last post by:
I am trying to display some database records in datagrid using dataset. the records need to be displayed are couple of thousands, but the records in database that the SQL query needs to exam are...
3
by: sbowman | last post by:
I have a database that I was using for Monthly Statistics. I've recently loaded hundreds of thousands of records of historical data. Now my queries are running really slow!! Is there anything I can...
4
by: Sharkiness | last post by:
Hi, I'm new to using queries in Access and am stuck on trying to run a query from a table. I have thousands of records with fields such as customer name, Company Name, Company Country and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.