worldnews — 2011-01-12T13:41:27-05:00 — #1
Say we have a list generated like:
SELECT * FROM users WHERE account_balance > 0
How would you then get the rank of the user = john_doe from this generated list? Without looping through this list to find the rank but have MySQL deliver it which would be more efficient than a For Loop to find the rank of a given user from this list.
r937 — 2011-01-12T13:51:44-05:00 — #2
SELECT ( SELECT COUNT(*) + 1
> t.account_balance ) AS rank
FROM users AS t
WHERE user = 'John Doe'
worldnews — 2011-01-12T16:06:02-05:00 — #3
Man, your SQL commands are always so complex I cannot quite decipher them
So let me instead give you the actual MySQL command and ask you to apply your suggestion to it. Here is the actual MySQL command from which I need to find the rank of a given User_id:
SELECT keyword, user_id, cost FROM keywords WHERE key_word LIKE'%web%' order by cost desc;
So once the above list is generated then for example I need to know the rank, aka the row number, of user_id = xyz
r937 — 2011-01-12T17:42:05-05:00 — #4
why do you want to generate a list?
i thought you wanted john doe's rank
what is the list for?
worldnews — 2011-01-12T20:02:48-05:00 — #5
Well we need to generate this list for various reasons, for all the reasons that one generates a list from a MySQL DB. From which list then we need to know what is the rank of X entry for various reasons.
John Doe thing was just a generic problem description, but since your code was too complex for me to make sense out of it, I decided best was to give you the actual select which generates the list.
r937 — 2011-01-12T20:43:21-05:00 — #6
okay, if you are creating a list, i don't understand why you can't compute the rank
just loop over the list with your application language and count the rows, or do an ArrayFind function or something
the information you want is in the list, there is no need to go back to mysql
worldnews — 2011-01-12T21:07:23-05:00 — #7
Well of course I can write the code in Php that would find the rank of a given row within the list, but I was wondering if there is a command in MySQL that would do that for us? I guess not. So off I go writing it in Php.
r937 — 2011-01-12T21:25:01-05:00 — #8
actually there is
except it isn't a command, it's a query (which i gave you earlier)
the reason to do it in php is for efficiency -- far quicker to extract something from data already in memory than to issue another call to mysql
furicane — 2011-01-13T07:09:11-05:00 — #9
Just a suggestion, why not calculate the rank or whatever you need when you insert data in the account_balance table? Update users table afterwards and you don't have to calculate anything on the fly as the data is pre-calculated so you just need to pull it out.
That way, you calculate once - it's more efficient, and you can work with the dataset easily, query it, order it and so on.
guelphdad — 2011-01-13T09:33:12-05:00 — #10
furicane, your suggestion works if the data in the table doesn't change. If it does, and that is the most likely scenario, you'd have to recalculate the rank so no time is saved. We're talking fractions of a second here. Also the data is already retrieved with the call to the database in php and the OP would be running a FOR loop or a WHILE so why not calculate it at that time with a variable in php?
furicane — 2011-01-13T09:55:45-05:00 — #11
Yes, when the data is changed you recalculate the value.
Then you ask yourself - do you have more requests to the database when running a report or item listing or do you have more CRUD operations.
Why would you calculate something on the fly when you can calculate it once? Why would you use PHP to perform any data sorting or calculation if you don't have to?
Database should take care of the data, not the underlying application.
r937 — 2011-01-13T10:24:09-05:00 — #12
that's a recipe for disaster
yup, and rank in this example is an application concern
in my opinion
furicane — 2011-01-13T11:01:26-05:00 — #13
If it's not a problem Rudy, can you please explain why it would be a recipe for disaster? I'm not trying to be rude, I'm just wondering what's the reasoning behind it.
r937 — 2011-01-13T11:12:44-05:00 — #14
okay, no problem
how is the recalculation triggered?
what happens if it doesn't work porpoerly?
is it possible for a "dirty" row to be retrieved before/while the recalculation is complete?
furicane — 2011-01-13T11:51:39-05:00 — #15
Just to ensure we're on the same page, I'll assume classic customer - invoices model.
1 customer can have multiple invoices. To simplify the whole process, let's assume that balance is a sum of unpaid invoices minus the sum of paid invoices (let's not go into debate about the table design, I'm just using this as an example).
So, to obtain John Doe's balance, we'd have to use aggregate functions to calculate the balance.
So, why not use a trigger that will update customers table with the sum of unpaid - paid?
That way, when we want to find out someone's balance, we don't have to calculate what the current sum is. Or, even better - we want to know the total that we are being owed - we can run a sum on customers table rather than sum on invoices table - it's less rows, hence it will be quicker.
Now, to answer the questions:
1) Recalculation is triggered by a trigger, on before insert, assuming that invoice isn't updated. If yes, the same procedure - trigger controlled.
2) I am not sure what you mean by not working properly. Yes, there are number of reasons why something can go wrong, but that's why we have the transactions available. As for the logic itself, if I am mistaken here - please, do correct me as I'd also like to know if it's wrong.
3) No, it isn't.