Solved

Slow SQL Server query in Delphi

Posted on 2009-04-04
6
599 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

912 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

26 Experts available now in Live!

Get 1:1 Help Now