Click here to Skip to main content
15,917,568 members
Home / Discussions / Database
   

Database

 
QuestionMySQL/ODBC Table will not Update from DataGrid [modified] Pin
][cecu3e2-Aug-07 6:52
][cecu3e2-Aug-07 6:52 
Questiondriver help Pin
boyindie2-Aug-07 4:56
boyindie2-Aug-07 4:56 
Questionreturning free numbers Pin
ArielR2-Aug-07 4:30
ArielR2-Aug-07 4:30 
AnswerRe: returning free numbers Pin
kubben2-Aug-07 7:22
kubben2-Aug-07 7:22 
AnswerRe: returning free numbers Pin
Michael Potter2-Aug-07 9:16
Michael Potter2-Aug-07 9:16 
GeneralRe: returning free numbers Pin
ArielR2-Aug-07 13:06
ArielR2-Aug-07 13:06 
GeneralRe: returning free numbers Pin
Michael Potter3-Aug-07 10:13
Michael Potter3-Aug-07 10:13 
GeneralRe: returning free numbers Pin
DQNOK3-Aug-07 10:32
professionalDQNOK3-Aug-07 10:32 
I agree with ArielR. Very nice. I'd have said it wasn't possible if I hadn't seen your solution.

Correct me if I'm wrong, but doesn't your solution only return the first and last numbers from a missing block...

1,2,3,9,10

It won't return all the missing numbers: 4 thru 8, will it? Just 4 and 8; right?

I also found it difficult to see how it was working, so I dumbed it down for my own understanding:
SELECT (T.ID - 1)  AS MissingID
FROM   tblMyTable AS T
WHERE  (T.ID - 1) NOT IN (SELECT ID FROM tblMyTable);

Of course, this will pick up numbers that don't belong in the table, like 0 (zero), so I added a predicate similar to yours:
SELECT (T.ID - 1)  AS MissingID
FROM   tblMyTable AS T
WHERE  (T.ID - 1) NOT IN (SELECT ID FROM tblMyTable)
  AND  T.ID  > @startnum;

Of course this ONLY picks up numbers of the form (ID-1) where ID IS in the table, and ID-1 is not. It won't pick up others numbers from a block of missing numbers.

Now a concern:

I *THINK* that each element from the (T.ID - 1) set will be subject to a linear scan of the entire (SELECT ID FROM tblMyTable) result set (well, it should stop as soon as a hit occurs). This will be an O(N^2) operation. I also *THINK* that if we just sorted the exclusion set:
WHERE  (T.ID - 1) NOT IN (SELECT ID FROM tblMyTable ORDER BY ID)

the DBMS should recognize the order, and do a binary search instead of a linear scan, reducing the overall select to O(N*ln(N)) (of course the ordering operation itself may be an O(N*ln(N)) operation). This may seem insignificant, but if you're searching a million record table, N^2 could be huge! In this case, N*ln(N) could be 50,000 times faster than N^2.

Then I began wondering if it could be reduced it to an O(N) operation just like a person would code it manually in a procedural language if they knew the two sets were ordered. Perhaps a LEFT JOIN like kubben suggested would do it:

SELECT Missing.ID FROM
 (SELECT (ID - 1) AS ID
  FROM   tblMyTable
  WHERE  ID  > @startnum
  ORDER BY ID) AS Missing
  LEFT JOIN 
 (SELECT ID
  FROM   tblMyTable
  ORDER BY ID) AS Includes ON Missing.ID = Includes.ID
WHERE Includes.ID IS NULL;

Maybe someone who knows more about the guts of a DBMS (like Mike Dimmick) could set me straight here. I'd have liked to run some speed tests with huge tables, but just don't have the resources available to me.

David
GeneralRe: returning free numbers Pin
Michael Potter3-Aug-07 13:35
Michael Potter3-Aug-07 13:35 
QuestionSimple E-mail functionality Pin
Bonsta2-Aug-07 4:26
Bonsta2-Aug-07 4:26 
AnswerRe: Simple E-mail functionality Pin
Sunset Towers2-Aug-07 7:21
Sunset Towers2-Aug-07 7:21 
QuestionCounting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 3:25
Sunset Towers2-Aug-07 3:25 
AnswerRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 3:29
kubben2-Aug-07 3:29 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 3:37
Sunset Towers2-Aug-07 3:37 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 5:10
Sunset Towers2-Aug-07 5:10 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 5:22
kubben2-Aug-07 5:22 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 5:43
kubben2-Aug-07 5:43 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 6:01
Sunset Towers2-Aug-07 6:01 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 6:29
kubben2-Aug-07 6:29 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 7:18
Sunset Towers2-Aug-07 7:18 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 7:20
kubben2-Aug-07 7:20 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 7:22
Sunset Towers2-Aug-07 7:22 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 7:25
kubben2-Aug-07 7:25 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
Sunset Towers2-Aug-07 7:37
Sunset Towers2-Aug-07 7:37 
GeneralRe: Counting the Records in a Database for a Insert Statement Pin
kubben2-Aug-07 7:51
kubben2-Aug-07 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.