473,396 Members | 1,967 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.

How to Divide query into pieces

TheSmileyCoder
2,322 Expert Mod 2GB
Details:
I have a temporary table which does contain a unique ID. I have a query that runs to update a field in the table. That query takes quite a long while to run, and as such my progress indicator appears to be at a standstill.

Therefore I was considering an approach of dividing it into steps, I.e. run a query that updates the first 10% percent, update progress indicator, run the next 10% and so on. I realise this approach will overall be slower, but it will not make the screen appear frozen.

Question: Is there something similar to Select Top 100 records but instead is Select records 101-200?


Alternatively I could do Select top 100 ... WHERE MyField Is not Null but before going that route I was curios to know if the other is possible.
Jan 31 '13 #1

✓ answered by Rabbit

The gaps won't matter. Something like this.
Expand|Select|Wrap|Line Numbers
  1. LastRunPK = 0
  2.  
  3. UPDATE table
  4. SET field = value
  5. WHERE PK IN (
  6.    SELECT TOP 100 PK
  7.    FROM table
  8.    WHERE PK > LastRunPK
  9.    ORDER BY PK
  10. )
  11.  
  12. Update progress meter.
  13.  
  14. LastRunPK = 
  15.    SELECT MAX(PK) AS MaxPK
  16.    FROM (
  17.       SELECT TOP 100 PK
  18.       FROM table
  19.       WHERE PK > LastRunPK
  20.       ORDER BY PK
  21.    ) AS t
Lather, rinse, repeat.

8 1459
Seth Schrock
2,965 Expert 2GB
From what I have found in a quick Google search, you can't select a range from the middle using the SELECT TOP function.

One idea that I did find was to create a field that did a running record count and use a BETWEEN criteria. However, I would think that this would slow down your query even more and isn't that much different then your solution of WHERE MyField Is Not Null.
Jan 31 '13 #2
Rabbit
12,516 Expert Mod 8TB
What's the SQL for your update query? Maybe we can optimize that.

As far as dividing it up, if you have a primary key, you could to a top 100 ordering by the primary key and that can serve as your cursor.
Jan 31 '13 #3
TheSmileyCoder
2,322 Expert Mod 2GB
As far as dividing it up, if you have a primary key, you could to a top 100 ordering by the primary key and that can serve as your cursor.
I don't really see how you could about doing this? Using it as a cursor?

Mind the primary key has plenty of holes, but of course no duplicats. I.e. an export could be records with primary key 1,2,3,4,5,127,128, 1009,10472.
Jan 31 '13 #4
Rabbit
12,516 Expert Mod 8TB
The gaps won't matter. Something like this.
Expand|Select|Wrap|Line Numbers
  1. LastRunPK = 0
  2.  
  3. UPDATE table
  4. SET field = value
  5. WHERE PK IN (
  6.    SELECT TOP 100 PK
  7.    FROM table
  8.    WHERE PK > LastRunPK
  9.    ORDER BY PK
  10. )
  11.  
  12. Update progress meter.
  13.  
  14. LastRunPK = 
  15.    SELECT MAX(PK) AS MaxPK
  16.    FROM (
  17.       SELECT TOP 100 PK
  18.       FROM table
  19.       WHERE PK > LastRunPK
  20.       ORDER BY PK
  21.    ) AS t
Lather, rinse, repeat.
Jan 31 '13 #5
TheSmileyCoder
2,322 Expert Mod 2GB
It was the getting of the LastRunPK that was eluding me. Thanks for the suggestion.
Jan 31 '13 #6
Rabbit
12,516 Expert Mod 8TB
Not a problem. I am still of the mindset that optimizing the update query, if any such optimization can be done, may remove the need to divide the query in this fashion.
Jan 31 '13 #7
zmbd
5,501 Expert Mod 4TB
If PK is numeric use the "Between" along with the Primary key.
I have a VBA and an UPDATE - SQL that does just this sort of thing for the exact same reason. I then set the built in status bar using some code I picked up along the way.

If I was at work I'd post you the SQL; however, the basics are built from the following:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblpuke1.id, 
  2.    tblpuke1.puke, 
  3.    tblpuke1.puke2
  4. FROM tblpuke1
  5. WHERE (((tblpuke1.id) Between 1 And 3));
  6.  
  7.  
Convert this to an UPDATE Query
Expand|Select|Wrap|Line Numbers
  1. UPDATE tblpuke1 
  2.    SET tblpuke1.puke = "NewValue"
  3. WHERE (((tblpuke1.id) 
  4.    Between 1 And 3));
  5.  
Instead of the hard coded Between condition, build the string in VBA.
Several ways to get the update increment for the status bar.
Normally, I open the record set sorted on the PK, move last, get that value. I then use a for next loop, starting my strings there and use an increment against that loop and test against the last value and drop the loop after the high value.
At each cycle of the loop I update the status bar. In V2003 I had to issue a doevents command.

This may be along the same lines as Seth's suggestion.

And that is one of the reasons I use that autonumber as a PK for every table, even when a composite key will do the job... makes things like this easy.
Feb 2 '13 #8
TheSmileyCoder
2,322 Expert Mod 2GB
Thank you all for your replies. I think you (Rabbit) are right that I could aim for some improvements into the query design, but I do believe that is a question best suited for its own thread.
Feb 5 '13 #9

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

Similar topics

0
by: Gary Carson | last post by:
Can anyone tell why the query below would throw a divide-by-zero error? The only reason I can see for the error happening would be if SUM() came out to be zero, but this never happens with the...
5
by: Otie | last post by:
I have a select query that groups records together, specifically baseball players and their home runs and at-bats. I want to create a query that displays , , and and that sorts ASCENDING on the ...
3
by: KishenGajjar | last post by:
I'm trying to convert a query that runs in Query Analyser (SQL Server 2000) to work in MS Access. This will then help me modify the query further and use it to create reports and such. This query...
2
by: programmo | last post by:
I need to divide in two string an article, to split in more pages: I have this string: <hello word> <hello word2>
3
by: pukhton | last post by:
Need help in building a query. I have a Main Table, and it has Colums (Medication_Name, Diluent Type, Total Dose, Concentration), now I want a query where I can divide (Total Dose/Concentration)...
18
by: cov | last post by:
I have a query where I'm attempting to pull data from 3 different tables using php and mysql. I had hoped to have a unique identifier to help ensure referential integrity but it appears that...
7
by: cov | last post by:
I have a php query where I'm attempting to pull data from 3 different tables between a php form and mysql db. I had hoped early on to use a unique identifier to help ensure referential integrity...
3
by: =?Utf-8?B?UnVieSBOYWRsZXI=?= | last post by:
Hi EveryOne, Does some one knows a way or tool that knows how to divide image into regions with c# so if the image is like a table style i will now the positions of the columns? i can read the...
2
by: JenavaS | last post by:
Hi all, I have a query: SELECT Data.Region, Data.Dept, Data.Year, Data.Month, Data.Week, Data.Elapsed, Data. AS WpComp, Data. AS CpComp, IIf(=53 And ="JAN,WK4",/2,) AS AdjLyComp,...
8
by: munkee | last post by:
Hi people, Im struggling with this quite hard to explain database query. How would I count only once the (many) occurance of a record within a one to many relationship. I have the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.