Solved

Slow SQL Server query in Delphi

Posted on 2009-04-04
6
597 Views
Last Modified: 2013-11-23
I am creating a simple Delphi 7 app to compare some records in an Access table with those in a SQL Server 2008 table. The Access table has 2600 records and I loop through each of them looking for a record in the SQL table with the same value in 2 fields. For some reason it is running very slowly and when I put some timing code in I found that after the first 50 or so records, the time to excecute the query keeps increasing. Eventually it was taking more than 1.5 seconds for each instance. The code is below.

tblContacts is the Access table and Qry1 runs on the SQL Server. The query is cursor location clUseClient, cursor type ctOpenForwardOnly, lock type ltReadOnly. The Attorneys table is indexed on a combination of the 2 search fields (FirstName + LastName).

I found that if I used a Locate on the SQL Server table it was about 5 times faster. But why would the query be so slow and keep getting slower?

tblContacts.First;

  while not tblContacts.EOF do begin

    Qry1.Close;

    Qry1.SQL.Add('SELECT * FROM Attorneys WHERE Firstname=' + QuotedStr(FirstName));

    Qry1.SQL.Add('AND Lastname=' + QuotedStr(tblContactsLastName.AsString));

    Qry1.Open;

    // Code to add matching names to a list will be here

    tblContacts.Next;

  end;

Open in new window

0
Comment
Question by:galcott1
  • 4
6 Comments
 
LVL 25

Expert Comment

by:reb73
ID: 24069737
You would get much better performance if you setup a linked server against your MS-Access database in SQL Server 2008..

No looping code is gonna ourperform a set based query as such..


0
 
LVL 4

Accepted Solution

by:
jamerslong earned 125 total points
ID: 24069751
Try this and see what happens, if you keep on adding stuff to the SQL.Text it will just keep slowing it down.
Closing the Query donsent clear the text box if i am mistaken.

try moving the Qry1.Close and Open outside of the loop unless your worried about the data changing durring the search.

    Qry1.SQL.Text := 'SELECT * FROM Attorneys WHERE Firstname=' + QuotedStr(FirstName);
    Qry1.SQL.Add('AND Lastname=' + QuotedStr(tblContactsLastName.AsString));

tblContacts.First;

    Qry1.Open;

  while not tblContacts.EOF do begin

    Qry1.SQL.Text := 'SELECT * FROM Attorneys WHERE Firstname=' + QuotedStr(FirstName);

    Qry1.SQL.Add('AND Lastname=' + QuotedStr(tblContactsLastName.AsString));

    // Code to add matching names to a list will be here

    tblContacts.Next;

  end;

    Qry1.Close;

Open in new window

0
 
LVL 4

Expert Comment

by:jamerslong
ID: 24069758
opps small change
    Qry1.Open;

tblContacts.First;

  while not tblContacts.EOF do begin

    Qry1.SQL.Text := 'SELECT * FROM Attorneys WHERE Firstname=' + QuotedStr(FirstName);

    Qry1.SQL.Add('AND Lastname=' + QuotedStr(tblContactsLastName.AsString));

    // Code to add matching names to a list will be here

    tblContacts.Next;

  end;

    Qry1.Close;

Open in new window

0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 4

Expert Comment

by:jamerslong
ID: 24069776
sorry i'm tired and realized i am mistaken
you cant move the Open and close outside fo the loop or it wont update so just inore that.

so you are just using the SQL to filter and taking the results to another place...

then you should do this
tblContacts.First;

  while not tblContacts.EOF do begin

    Qry1.Close;

    Qry1.SQL.Text := 'SELECT * FROM Attorneys WHERE Firstname=' + QuotedStr(FirstName); // instead of add you replace the current text

    Qry1.SQL.Add('AND Lastname=' + QuotedStr(tblContactsLastName.AsString));            //then you add the next line

    Qry1.Open;                                                                          //Open the Database

//do the magic                        

//tblContacts.Next

  end;

Open in new window

0
 
LVL 4

Expert Comment

by:jamerslong
ID: 24069791
Qry1.Close;
    Qry1.SQL.Text := 'SELECT * FROM Attorneys WHERE Firstname=' + QuotedStr(FirstName) + ' AND Lastname=' + QuotedStr(tblContactsLastName.AsString); // you
Qry1.Open;
could also do this.
0
 

Author Closing Comment

by:galcott1
ID: 31566674
Thanks for this. I realized that I made a dumb mistake by not clearing the SQL text between each repetition. I've done this kind of thing hundreds of times and I always include "SQL.Clear'; this time I forgot.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now