Latest Uploads
Extraterre ... .0.1 (zip)

dantheman363

Monty Teas ... Screenie 1

steve_ancell

Santa Clau ... ed his bag

waroffice

manic_platdude.png

spinal

Tetris Clone

steve_ancell

Super blues bros.

spinal

Forum Home

MySQL - order by in a different order?

UserMessage
Posted : Thursday, 15 September 2011, 19:19 | Permalink | Mark Here
HoboBen


WW Entries : 9
I almost have this query working, however it gives the timestamps in the wrong order.

However, as I understand it group by has nothing to do with sorting (grouping always happens before ordering).

Highslide JS

In this image, I am trying to get a list of all articles, with the only the latest revision entry returned for each article. When two revision share an articleId, they are revisions of the same article.

I am also joining the user table to get the user.name based on the revision.userId/user.id keys (this part is working).

So right now, I am successfully returning only one revision for each articleId. But I am, however, only returning the oldest revision.

How would I amend my query to do the same for the newest revision for each article?

Thanks -- I hope that made sense!


---

Reading around, it seems I will probably have to embed a query inside this query somehow.

-----
github
Posted : Thursday, 15 September 2011, 19:44 | Permalink | Mark Here
HoboBen


WW Entries : 9
I think I have this fixed:

-->

A bit ugly, considering how easy the first was. However I'm happier not using "group by" as it's a dodgy command.

-----
github
Posted : Friday, 16 September 2011, 11:32 | Permalink | Mark Here
HoboBen


WW Entries : 9
Dunno if anyone will know the answer to this, but is that embedded subquery going to run for every row in the table?

-----
github
Posted : Friday, 16 September 2011, 12:08 | Permalink | Mark Here
JL235


I'd be pretty sure it won't run on every row.
Posted : Friday, 16 September 2011, 12:51 | Permalink | Mark Here
HoboBen


WW Entries : 9
Benchmarking, for 10,000 revisions of 50 articles:

select revision1.*, user.id, user.name
from revision revision1, user
where user.id = revision1.userId
and revision1.revisionTime =
(select max(revision2.revisionTime)
from revision revision2
where revision2.articleId = revision1.articleId)
order by revision1.publishedDate desc
51 rows in set (8.04 sec) // can reduce to 6.75 sec by comparing revision.id instead of revision.revisionTime


select revision.*, user.id, user.name from revision, user where revision.userId = user.id group by revision.articleId;
50 rows in set (0.11 sec)


8.04 seconds vs 0.11 seconds... a bit worrying.

Also, I have no idea why but I have two rows for article.id=36 in the first query! Additionally the second (faster) query doesn't actually pick the correct userId.

Bloody databases.


-----


Good news though, it appears the query is cached -- it takes 0.0 seconds if you call it twice in a row! That helps slightly, but it won't always work. Any suggestions on how I might rewrite the query?

Perhaps it's time to denormalise and have a second table.

-----
github
Posted : Friday, 16 September 2011, 13:48 | Permalink | Mark Here
HoboBen


WW Entries : 9
Sod it.

-->

select revision.* from article, revision where article.revisionId = revision.id;
50 rows in set (0.00 sec)

Slightly more work for PHP to do (and hence more places where the database will have to be locked by PHP to ensure correctness), but it's quicker and simpler than the alternative!

-----
github
Latest Posts
Shoutbox Topic - 976
Afr0 Sun 00:19
Audio Rant
steve_ancell Sat 19:16
Wrong Partition!!!!?
spinal Sat 11:24
Progress / Location Bars
Afr0 Sat 03:15
eBay Lies
spinal Fri 23:44
Shoutbox Topic - 968
dna Fri 19:42
Development via GUI
JL235 Fri 07:59
Meh
Afr0 Fri 04:13
Randomly distributed coordinates...
Afr0 Fri 00:20
Mog-Dog II : Electric Boogaloo
steve_ancell Thu 17:26
More

Latest Items
News : Newsletter #176
Jayenkai Sat 04:49
News : Newsletter #175
Dabz Tue 09:38
Blog : Snow: More Material Junk
Cower Sat 23:17
Dev-Diary : Mutant Monty: Amstrad CPC to Windows conversion
rockford Fri 13:14
Techy : AppleTV
Jayenkai Thu 09:40
Blog : Graphviz
steve_ancell Sat 14:17
Pets : Top-Down Shadow Hack
Jayenkai Tue 05:52
Snippet : JNKrunch v1.0
Jayenkai Sat 07:20
News : Newsletter #173
waroffice Fri 04:47
Blog : Material Loading
Cower Fri 02:08
Pets : I Done Won A Thing
shroom_monk Sun 11:31
Pets : Repurposing A Lexer
Cower Mon 22:06
Bah : Feeling a Little Angry
spinal Mon 11:26
News : Newsletter #170
Dabz Sat 00:34
Showcase : sbfgen
Cower Sat 16:57
More

Who's Online
HoboBen
Sun, at 01:47
shroom_monk
Sun, at 01:45
Dabz
Sun, at 01:31
Mog
Sun, at 00:59
Afr0
Sun, at 00:21
Cower
Sat, at 23:11
blanko1324
Sat, at 23:01
jprofitt
Sat, at 22:10
JL235
Sat, at 20:09
Evil Roy Ferguson
Sat, at 19:55
Link to this page
Site : Jayenkai 2006-Infinity |
MudChat's origins, BBCode's former life, Image Scaler.