Re: Left Join Question
От | ries van Twisk |
---|---|
Тема | Re: Left Join Question |
Дата | |
Msg-id | 769E2C10-DC5B-4D06-8D3F-4AC70EB2E022@rvt.dds.nl обсуждение исходный текст |
Ответ на | Left Join Question ("Ryan Wells" <ryan.wells@soapware.com>) |
Список | pgsql-sql |
On Nov 18, 2008, at 5:48 PM, Ryan Wells wrote:
While looking through our data layer code today, I ran across this query:SELECTtasks.*,clients.FirstName,clients.LastName,clients.MiddleInitial,iteminfo.CreatedBy,iteminfo.StationId,iteminfo.CreatedDate,changelog.LastModified,changelog.LastModifiedBy,changelog.LastModifiedAt,ticklers.Due,ticklers.ReminderFROM tasksLEFT JOIN clients ON tasks.ClientId = clients.ClientIdLEFT JOIN iteminfo ON tasks.Id = iteminfo.ItemIdLEFT JOIN changelog ON tasks.Id = changelog.ItemIdLEFT JOIN ticklers ON tasks.Id = ticklers.RelatedIdWHERE tasks.Id = '123456';(I've cleaned it up so it's easier to read.)The basic data structure is that we have a todo list that contains a list of tasks which may or may not be associated with clients, items, log entries, or ticklers (scheduled reminders).The query works as intended: it returns a result-set with all the necessary data to display in the todo list. The performance is not a major concern, although it can be slow for large lists.Since it works, my question is really more about principles: Given that each of the tables in question will contain tens of thousands of rows, is a nested join really the best way to approach this?Thanks!Ryan
To answer this better we need to know the exact relations between each table.
For example, does each task have 0 or more clients, or 1 or more clients?
If it's 1 or more, then you can properly use a JOIN instead of a LEFT JOIN, this would make the operation faster I believe.
I hope I did explain myself correctly.
Ries
В списке pgsql-sql по дате отправления: