![]() |
Complex SQL Question Question
Hey guys; Trying to optimize some code that I wrote to not kill our DB server, I was hoping someone could give me some insightful feedback, as googling this isn't very easy.
Basically I have some cats in a database table, all with their own id. Each cat gets certain vaccinations, but never all at once. So I have another table where a record is inserted in a column indicating which shots they have received and when. There is a single column for each of these vaccinations, I put a "y" in it when complete. So I want to generate a report which displays shots not administered, right now I grab each cat id, and loop through each one finding which ones are done, and which are not. I am hoping I can take a single query and filter out results which have a Y in each column, problem is that this can be over multiple records. This will save a HUGE load on the DB. Any input? Thanks,. |
Re: Complex SQL Question Question
Could you clarify your question, or how the tables are set up, so someone can give the right answer? ;) Maybe even an example of what's in the DB would be great.
But I would have the tables set up so I can just do something like: SELECT cat_id, vaccination_id FROM shots_table WHERE complete = 'n' ORDER BY cat_idThen each row will be all the shots that the cat didn't receive, for each cat, ordered by the cat that didn't receive it. That is if this is how your tables are set up. But this seems way too simple an answer. |
Re: Complex SQL Question Question
assuming one cat has many vaccinations and you want to find cats without any vaccinations
:
|
Re: Complex SQL Question Question
Not knowing how many columns there are, is it feasible to do something like this:
:
Otherwise, what if you redid the database like this: :
Cats (Id (PK), Name, Owner, whatever):
Of course, this is technically 2 queries, but the inner one will be quick (the DB will know how many rows are in the table so this query is easily optimized internally). |
Re: Complex SQL Question Question
I always thought that LEFT OUTER JOIN required matching records in both tables, now that I know it does not, it should actually work very well. Thanks.
|
Re: Complex SQL Question Question
Ok, so officially this is what solved my problem, and despite the number of inner queries, it runs much much faster...
:
SELECT DISTINCT cat_id, cat_name, cst_status, cat_birthday, cat_intake_date, CONVERT(VARCHAR(10), cat_birthday, 111) AS [YYYY/MM/DD] |
Re: Complex SQL Question Question
try modifying your query to use this approach. i have no way of testing this, but i think you could eliminate those subqueries
:
|
Re: Complex SQL Question Question
So, are the counts ever going to be more than 1? If so, presumably that means that either the cat will have to get another one in the future and you maybe should somehow be tracking the date it was last administered, unless getting the shot extra times is optional. If not, then using the inner query will be a performance cost, as you're doing a join and a projection for each sub-query when you could just do the join once and compare to each column.
|
Re: Complex SQL Question Question
Quote:
I should add that even when using multiple subqueries this improved DB performance by a massive amount. |
Re: Complex SQL Question Question
i suspect that your database is not normalized
|
| All times are GMT -5. The time now is 3:37 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC