If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Other > Left Join

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-09-09, 03:18
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Left Join

I have 2 SQLite3 tables like this

table persons (~15,000 records)
ID, Name, Address
ID is PK

table OtherInfo (~10,000 records)
ID, personID, InfoNumber
ID is PK, personID is FK and UNIQUE

Code:
SELECT COUNT(p.ID)
FROM persons p LEFT JOIN OtherInfo oi ON oi.personID = p.ID
WHERE <cond>

SELECT p.ID, p.Name, oi.InfoNumber
FROM persons p LEFT JOIN OtherInfo oi ON oi.personID = p.ID
WHERE <cond>
Even though there is just one InfoNumber for each person, it wasnt created in the person table inorder to save space (in the actual table, there are many more VARCHAR fields).

But now, the LEFT JOIN query is taking a whole lot of time, so I may have to restructure the tables to have all fields in a single table.

But is there a way out for the LEFT JOIN query ?

Thanks
Reply With Quote
  #2 (permalink)  
Old 06-09-09, 07:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,084
what does "a way out" mean?

did you declare an index on personID?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-09-09, 10:43
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
I initially create index on 2 columns :

CREATE UNIQUE INDEX IF NOT EXISTS "UID" ON "OtherInfo" (personID, InfoNumber);

But have changed that to personID UNIQUE only. Now not so slow.

But as more data is added, LEFT JOIN query is going to be slower. I was wondering if there was to optmize it instead of restructuring the table altogether.
Reply With Quote
  #4 (permalink)  
Old 06-09-09, 16:25
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
You have an ID column on OtherInfo. Is that necessary? Why not make PersonID the key since you know it will be unique?
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On