Efter at have set de andre svar igennem hjalp det mig med at indse et underliggende problem.
Artikeltabellen og article_images-tabellen skal begge have et ekstra indeks.
Dette er, hvad du har nu:
CREATE TABLE `articles` ( `id` int(10) unsigned not null auto_increment, `author_id` int(10) unsigned not null, `date_created` datetime not null, PRIMARY KEY(id)) ENGINE=InnoDB;CREATE TABLE `article_images` (`article_id` int(10) unsigned not null, `filename` varchar(100) not null, `date_added` datetime not null, UNIQUE INDEX(article_id, filename)) ENGINE=InnoDB;
Dette har du brug for:
CREATE TABLE `artikler` ( `id` int(10) unsigned not null auto_increment, `author_id` int(10) unsigned not null, `date_created` datetime not null, PRIMARY KEY(id), INDEX ( date_created DESC)) ENGINE=InnoDB;CREATE TABLE `article_images` (`article_id` int(10) unsigned not null, `filename` varchar(100) not null, `date_added` datetime not null, UNIQUE INDEX (article_id, filename), INDEX (article_id, date_added),) ENGINE=InnoDB;
artikler
Det nye indeks til at bestille artiklerne efter indsættelsesdato i faldende rækkefølge
article_images
Det første indeks er stadig nødvendigt, da det vil beskytte mod at vedhæfte et andet billede med samme filnavn til en artikel.
Det andet indeks vil gøre det nemt at finde det seneste billede,
Med disse nye indekser på plads, er her forespørgslen, der vil gøre dit bud:
VÆLG AAA.author_id, AAA.date_created, IFNULL(BBB.title,'') titel, IFNULL(CCC.filename,'') filnavn FROM ( SELECT AA.id , AA.date_added, BB.author_id, BB.date_created FROM ( SELECT A.id,IFNULL(MAX(B.date_added),'1900-01-01 00:00:00') date_added FROM (SELECT id FROM articles ORDER BY date_created DESC LIMIT 40) A LEFT JOIN article_images B ON A.id =B.article_id GROUP BY A.id ) AA INNER JOIN artikler BB USING (id) ) AAA LEFT JOIN article_contents BBB ON AAA.id=BBB.article_ID article_LEFT CCC TIL (AAA.id=CCC.article_id OG AAA.date_added=CCC.date_added) BESTIL AF AAA.date_created DESC;
Målet er først at oprette en intern underforespørgsel, der kun har 40 rækker. Det er det, AA bør bringe tilbage. Underforespørgsel AAA skal have artikeloplysningerne plus datoen for det sidste billede blev indsat. Det sidste sæt joins skal forbinde de 40 id'er med en titel og et billede. Det sidste trin er at præsentere resultatet i faldende rækkefølge.
Da jeg ikke har prøvedata, skrev jeg tre lagrede procedurer i en prøvedatabase kaldet stuff
1 for at lave tabellerne
DELIMITER $$DROP PROCEDURE HVIS FINDER `ting`.`MakeTables` $$CREATE PROCEDURE `stuff`.`MakeTables` ()BEGIN DROP TABEL HVIS EKSISTERER artikler;DROP TABLE HVIS FINDER article_contents;s EXISTS TABLE article;CREATE TABLE `artikler` ( `id` int(10) unsigned not null auto_increment, `author_id` int(10) unsigned not null, `date_created` datetime not null, PRIMARY KEY(id), INDEX (date_created DESC)) ENGINE =InnoDB;CREATE TABLE `article_contents` (`article_id` int(10) unsigned not null, `title` varchar(100) not null, `content` text not null,PRIMARY KEY(article_id)) ENGINE=InnoDB;CREATE TABLE ` article_images` ( `article_id` int(10) unsigned not null, `filename` varchar(100) not null, `date_added` datetime not null, UNIQUE INDEX (article_id, filename), INDEX (article_id, date_added)) ENGINE=InnoDB; AFSLUT $$DELIMITER;
1 for at lave dataene
DELIMITER $$DROP PROCEDURE HVIS FINDER `stuff`.`LoadSampleData` $$CREATE PROCEDURE `stuff`.`LoadSampleData` ()BEGIN DECLARE x,y,z INT; SET x =1; MENS x <=100 INDSÆT I artikler (author_id,date_created) VALUES (RAND() * POWER(2,31), DATE('1970-01-01 00:00:00') + INTERVAL (RAND() * POWER (2,30)) ANDET); SET x =x + 1; SLUT MENS; SET x =1; WHILE x <=100 DO SET y =FLOOR(RAND() * 100); HVIS y>=30 SÅ INDSÆT INTO article_contents VALUES (x, CONCAT('TITLE_',FLOOR(RAND() * POWER(2,31))), CONCAT('CONTENT_',FLOOR(RAND() * POWER(2, 31)))); AFSLUT HVIS; SET x =x + 1; SLUT MENS; SET x =1; MENS x <=100 VÆLG COUNT(1) INTO y FROM article_contents WHERE article_id =x; HVIS y =1 SÅ SÆT y =0; MENS y <20 INDSTILLES y =y + 1; SET z =GULV(RAND() * 10); HVIS z>=5 SÅ INDSÆT INTO article_images VALUES (x, CONCAT('IMAGE_',FLOOR(RAND() * POWER(2,31))), DATE('1970-01-01 00:00:00') + INTERVAL (RAND() * POWER(2,30)) SEKUND); AFSLUT HVIS; SLUT MENS; AFSLUT HVIS; SET x =x + 1; SLUT MENS; VÆLG ANTAL(1) INTO x FRA artikler; VÆLG ANTAL(1) INTO y FROM article_contents; SELECT COUNT(1) INTO z FROM article_images; SELECT CONCAT('Artikler:',x,' Titler:',y,' Billeder:',z) Resultater;END $$DELIMITER;
1 for at køre forespørgslen
DELIMITER $$DROP PROCEDURE HVIS FINDER `stuff`.`ShowLast40` $$CREATE PROCEDURE `stuff`.`ShowLast40` ()BEGIN SELECT AAA.author_id, AAA.date_created, IFNULL(BBB.title,' ') title, IFNULL(CCC.filename,'') filnavn FROM ( SELECT AA.id, AA.date_added, BB.author_id, BB.date_created FROM ( SELECT A.id,IFNULL(MAX) (B.date_added),'1900-01-01 00:00:00') date_added FROM (SELECT id FROM articles ORDER BY date_created DESC LIMIT 40) A LEFT JOIN article_images B ON A.id =B.article_id GROUP BY A. id ) AA INNER JOIN artikler BB BRUG AF (id) ) AAA LEFT JOIN article_contents BBB ON AAA.id=BBB.article_id LEFT JOIN article_images CCC ON (AAA.id=CCC.article_id AND AAA.date_added=CCC) ORDER_ad AA .date_created DESC;END $$DELIMITER;
Her er et eksempel på output, da jeg kørte det på mit Windows-skrivebord (MySQL 5.5.12):
mysql> kalder maketables; call loadsampledata;Forespørgsel OK, 0 rækker påvirket (0,31 sek.)+------------------------------------------------ +| Resultater |+--------------------------------------------+| Artikler:100 Titler:67 Billeder:666 |+------------------------------------------+1 række i sæt (23,73 sek.)mysql> kald showlast40;+-----------------------+--- ---------------+------------------------+| forfatter_id | dato_oprettet | titel | filnavn |+------------+------------------- ------+-------------------------+| 1576679519 | 2004-01-02 14:05:17 | TITLE_1757853515 | IMAGE_1601858163 || 992840519 | 2003-05-17 05:48:11 | TITLE_319026350 | IMAGE_1913708631 || 1366977197 | 2003-03-20 19:37:23 | | || 1705517382 | 2003-02-07 16:48:56 | | || 1529899841 | 2002-11-02 20:59:26 | | || 1756824942 | 28-08-2002 16:01:59 | | || 175825630 | 08-05-2002 13:48:56 | TITLE_240812804 | IMAGE_1214850809 || 757530551 | 08-02-2002 00:20:17 | TITLE_4447486 | IMAGE_1511850161 || 840251261 | 25-01-2002 20:06:56 | TITLE_1160842143 | IMAGE_206027488 || 964653347 | 2001-12-26 19:15:47 | TITLE_1552408257 | IMAGE_717719932 || 2106039126 | 11-11-2001 17:09:29 | | || 2085991608 | 01-08-2001 12:48:20 | | || 1895462094 | 2000-12-02 05:31:41 | | || 1589384729 | 28-04-2000 23:55:50 | TITLE_1040850308 | IMAGE_1200414639 || 514341550 | 2000-04-20 07:25:05 | TITLE_188288840 | IMAGE_164856430 || 887359583 | 13-02-2000 03:30:47 | | || 1156687499 | 16-06-1999 00:29:17 | TITLE_686398996 | IMAGE_670200418 || 1561242593 | 1998-12-08 05:50:17 | | || 1117889993 | 1998-10-23 17:02:44 | TITLE_1491217221 | IMAGE_649630126 || 740063135 | 16-09-1998 23:52:41 | TITLE_579374776 | IMAGE_757313192 || 429699232 | 19-04-1998 01:41:17 | TITLE_73748980 | IMAGE_1881818111 || 1827051060 | 1998-02-27 01:01:50 | TITLE_1588619991 | IMAGE_1657322715 || 1442984429 | 19-11-1997 21:23:35 | TITLE_184173382 | IMAGE_597809368 || 152267158 | 02-05-1997 20:25:50 | | || 1323598169 | 14-03-1997 16:30:38 | TITLE_1355869397 | IMAGE_1058313818 || 66122740 | 05-01-1997 15:12:20 | TITLE_1259073183 | IMAGE_198280936 || 5161474 | 1996-06-28 10:47:26 | TITLE_1876022823 | IMAGE_1138098675 || 1865082792 | 1996-03-01 19:09:11 | TITLE_1288151615 | IMAGE_245974646 || 1923481146 | 07-08-1995 00:36:11 | TITLE_922744000 | IMAGE_2067090321 || 1725218958 | 18-03-1995 05:15:29 | TITLE_583120586 | IMAGE_592773824 || 117806248 | 05-01-1995 02:34:32 | | || 1428777335 | 1993-06-06 01:52:32 | TITLE_661148588 | IMAGE_633345518 || 1091245943 | 1993-06-05 05:51:47 | TITLE_1407444563 | IMAGE_538936256 || 2088382260 | 25-03-1993 06:03:29 | TITLE_1144364681 | IMAGE_1790013089 || 625878569 | 1992-12-21 07:41:26 | TITLE_1319355723 | IMAGE_921580624 || 110555110 | 01-01-1992 20:49:59 | | || 1110532475 | 1991-11-20 07:19:32 | | || 1726795146 | 09-10-1990 00:23:44 | TITLE_782624350 | IMAGE_1760322575 || 370183888 | 30-03-1990 15:59:17 | | || 1497483317 | 19-02-1990 01:25:41 | TITLE_776483948 | IMAGE_665824222 |+------------------------------- ------+------------------+40 rækker i sæt (0,00 sek.)
Prøv det!!!
OPDATERING
Jeg var helt sikker på, at de 40 datetimes, du læser, faktisk er top 40. Det er de. Jeg kørte forespørgslen:vælg * fra artikler ORDER BY date_created DESC; for at sikre.
OPDATERING 2011-05-17 14:06
mysql> kalder maketables; call loadsampledata;Forespørgsel OK, 0 rækker påvirket (0,45 sek.)+------------------------------------------------ -+| Resultater |+---------------------------------------------+| Artikler:100 Titler:67 Billeder:6739 |+--------------------------------------------+1 række i sæt (3 min. 45.45 sek.) Forespørgsel OK, 0 rækker påvirket (3 min. 45.45 sek.) mysql> call showlast40;+-------+---------- --------------------------+-------------------+ | forfatter_id | dato_oprettet | titel | filnavn |+------------+------------------- ------+-------------------------+| 196582776 | 05-01-2004 14:09:04 | | || 1880371016 | 31-07-2003 05:50:37 | TITLE_1191518827 | IMAGE_1562208019 || 22204986 | 16-02-2003 14:09:22 | | || 355490160 | 21-11-2002 02:35:19 | | || 869510149 | 27-12-2001 22:07:52 | TITLE_1528616779 | IMAGE_223327284 || 2063556512 | 16-04-2001 18:47:46 | TITLE_1839975091 | IMAGE_1282187005 || 529754190 | 14-07-2000 19:44:01 | TITLE_1557423205 | IMAGE_1931606737 || 166226262 | 1999-11-08 03:27:22 | | || 1981417562 | 11-09-1999 12:59:10 | TITLE_198262896 | IMAGE_1491273871 || 831057001 | 14-06-1999 15:06:31 | TITLE_1170272131 | IMAGE_760396200 || 1454252623 | 02-06-1998 08:35:46 | | || 1435450777 | 17-11-1997 18:10:34 | TITLE_482497458 | IMAGE_1331932705 || 1536315541 | 1997-11-02 05:24:49 | | || 2078028530 | 14-03-1997 22:36:58 | TITLE_321332010 | IMAGE_1897983295 || 701651581 | 13-01-1997 22:36:58 | TITLE_1337390701 | IMAGE_1630983859 || 101442444 | 22-11-1996 09:40:16 | | || 51114930 | 1996-11-20 03:24:49 | TITLE_1866751135 | IMAGE_1669595407 || 722056183 | 03-08-1996 15:23:01 | | || 1178720989 | 29-06-1996 22:47:19 | TITLE_579734376 | IMAGE_833229222 || 511355958 | 10-03-1996 09:32:46 | TITLE_1540275289 | IMAGE_1168117261 || 831921829 | 31-01-1996 06:36:04 | TITLE_661038882 | IMAGE_1199197195 || 1288455163 | 1995-08-22 00:34:25 | TITLE_1599332515 | IMAGE_822445764 || 1976208956 | 02-06-1995 09:23:01 | TITLE_58372998 | IMAGE_793318650 || 2092066982 | 28-03-1995 20:35:37 | TITLE_1693142377 | IMAGE_1176935479 || 1083841549 | 11-07-1994 18:46:52 | TITLE_1955674591 | IMAGE_1240485919 || 359037132 | 11-07-1994 02:44:19 | TITLE_713454936 | IMAGE_1072569732 || 1471985773 | 10-05-1994 17:08:01 | TITLE_1065017724 | IMAGE_393097704 || 1888864730 | 15-01-1994 17:41:28 | TITLE_1060275498 | IMAGE_230810100 || 1688028488 | 1993-12-28 06:36:58 | | || 1739777948 | 15-02-1993 00:30:31 | TITLE_1226842225 | IMAGE_1615058467 || 445721334 | 15-12-1991 20:54:49 | TITLE_1336145587 | IMAGE_2114729323 || 1661002442 | 30-06-1991 05:49:34 | TITLE_151142910 | IMAGE_1623325381 || 2092223006 | 13-06-1991 13:15:58 | TITLE_33175860 | IMAGE_1225117771 || 1553434585 | 1991-01-12 03:34:25 | TITLE_728483442 | IMAGE_1954153339 || 528544608 | 10-11-1990 08:21:04 | | || 1043927395 | 05-10-1990 00:48:49 | TITLE_304307448 | IMAGE_1702062493 || 1685702960 | 28-04-1990 05:44:19 | TITLE_1909853341 | IMAGE_263553036 || 1392428383 | 07-03-1990 15:08:46 | | || 643714153 | 14-02-1990 08:32:10 | TITLE_837416724 | IMAGE_1673964259 || 2132028206 | 28-09-1989 16:04:07 | TITLE_614908878 | IMAGE_1362210487 |+----------------------------------- ------+------------------+40 rækker i sæt (0,01 sek.)
Alt fungerer som jeg publicerede før, selv med et større sæt billeder. Her er forespørgslen fra ShowLast40 Stored Procedure:
VÆLG AAA.author_id, AAA.date_created, IFNULL(BBB.title,'') titel, IFNULL(CCC.filename,'') filnavn FROM ( SELECT AA.id , AA.date_added, BB.author_id, BB.date_created FROM ( SELECT A.id,IFNULL(MAX(B.date_added),'1900-01-01 00:00:00') date_added FROM (SELECT id FROM articles ORDER BY date_created DESC LIMIT 40) A LEFT JOIN article_images B ON A.id =B.article_id GROUP BY A.id ) AA INNER JOIN artikler BB USING (id) ) AAA LEFT JOIN article_contents BBB ON AAA.id=BBB.article_ID article_LEFT CCC TIL (AAA.id=CCC.article_id OG AAA.date_added=CCC.date_added) BESTIL AF AAA.date_created DESC;
OPDATERING 2011-05-17 15:16
Bare for at grine øgede jeg prøven og kørte den igen. Samme forespørgselshastighed.
mysql> kalder maketables; kalde loadsampledata; ring til showlast40;Forespørgsel OK, 0 rækker påvirket (0,38 sek.)+------------------------------------------------ ----+| Resultater |+----------------------------------------+| Artikler:1000 Titler:886 Billeder:88596 |+------------------------------------------------ -+1 række i sæt (51 min. 22,29 sek.) Forespørgsel OK, 0 rækker påvirket (51 min. 22,29 sek.)+-------+------ ----------+------------------------+------------------------+| forfatter_id | dato_oprettet | titel | filnavn |+------------+------------------- ------+-------------------------+| 1464539515 | 2004-01-07 22:45:04 | TITLE_676010724 | IMAGE_1877060293 || 272558724 | 03-01-2004 23:44:19 | TITLE_1213504045 | IMAGE_550812606 || 2000476448 | 28-12-2003 18:05:10 | TITLE_1762951489 | IMAGE_1201290847 || 955209697 | 25-12-2003 00:55:43 | TITLE_1064749344 | IMAGE_1335865507 || 1657429856 | 19-12-2003 01:03:13 | TITLE_1931852743 | IMAGE_905288424 || 759381001 | 12-11-2003 10:46:52 | TITLE_878255772 | IMAGE_2014780795 || 1269478951 | 06-11-2003 02:06:22 | TITLE_2026098781 | IMAGE_982272966 || 1049672131 | 04-10-2003 20:55:34 | TITLE_2043080215 | IMAGE_987859662 || 1429108729 | 16-09-2003 19:07:52 | TITLE_424483080 | IMAGE_35379150 || 1672198676 | 13-09-2003 11:49:52 | TITLE_1131552745 | IMAGE_875049630 || 1645878842 | 24-08-2003 13:42:04 | TITLE_1077302833 | IMAGE_702269538 || 172347180 | 2003-08-21 14:26:37 | TITLE_558691044 | IMAGE_1091183587 || 1137674509 | 2003-08-15 08:44:37 | TITLE_1982979709 | IMAGE_1234487941 || 282998112 | 05-08-2003 10:01:34 | TITLE_353831568 | IMAGE_738487608 || 246145344 | 02-08-2003 00:42:31 | TITLE_376954044 | IMAGE_1279375459 || 218409162 | 14-07-2003 02:55:16 | TITLE_1932540991 | IMAGE_1078689211 || 593263087 | 12-07-2003 22:47:01 | TITLE_1604012533 | IMAGE_834822870 || 2115914174 | 06-07-2003 03:06:31 | TITLE_1268165545 | IMAGE_1068632322 || 552557275 | 01-07-2003 16:45:22 | TITLE_2022112717 | IMAGE_1410588295 || 1500437041 | 29-06-2003 20:05:19 | TITLE_35559258 | IMAGE_159953586 || 1098371257 | 09-06-2003 07:29:37 | TITLE_1694076415 | IMAGE_1409619391 || 1570373503 | 22-05-2003 16:45:04 | TITLE_125157894 | IMAGE_723393492 || 1330507411 | 05-05-2003 21:40:07 | TITLE_1571250589 | IMAGE_701840418 || 1666035620 | 26-04-2003 02:51:40 | TITLE_504713706 | IMAGE_1410357553 || 1458179791 | 2003-04-19 05:34:25 | | || 1365758305 | 2003-03-28 10:09:58 | TITLE_704554170 | IMAGE_2085080137 || 2131082774 | 26-03-2003 16:43:25 | TITLE_1411034929 | IMAGE_303539208 || 103396632 | 2003-02-14 09:11:28 | TITLE_915927396 | IMAGE_1381045723 || 396479202 | 01-02-2003 15:51:40 | | || 2019916250 | 30-01-2003 00:44:46 | | || 431091906 | 29-01-2003 13:08:37 | | || 705166549 | 23-01-2003 21:37:07 | TITLE_1530318643 | IMAGE_257673696 || 1278327049 | 31-12-2002 16:39:40 | TITLE_772845324 | IMAGE_1355754913 || 1871174528 | 30-12-2002 19:03:40 | TITLE_65725764 | IMAGE_522904938 || 611892727 | 22-12-2002 10:19:07 | TITLE_333758274 | IMAGE_734815032 || 758497849 | 04-12-2002 15:05:10 | TITLE_129140574 | IMAGE_244407066 || 518111034 | 17-10-2002 16:38:10 | TITLE_1976498683 | IMAGE_2008599775 || 1737725786 | 15-10-2002 23:52:52 | TITLE_1441053871 | IMAGE_1595265847 || 1206055789 | 03-10-2002 22:07:52 | TITLE_562697952 | IMAGE_198940092 || 702790153 | 31-08-2002 17:37:16 | TITLE_1788304903 | IMAGE_1212944101 |+-------------------------------- ------+------------------+40 rækker i sæt (0,01 sek.)Forespørgsel OK, 0 rækker påvirket (0,01 sek.)
OPDATERING 2011-05-17 15:40
Her er det nye output inklusive datoen for det seneste billede:
mysql> kalder showlast40;+------------+---------------------------+--- ---------------+-------------------+--------------- ------+| forfatter_id | dato_oprettet | titel | filnavn | billeddato |+------------+---------------------+------------ ------+------------------+---------------------+| 1464539515 | 2004-01-07 22:45:04 | TITLE_676010724 | IMAGE_1877060293 | 16-10-2003 02:06:58 || 272558724 | 03-01-2004 23:44:19 | TITLE_1213504045 | IMAGE_550812606 | 2003-12-28 07:25:43 || 2000476448 | 28-12-2003 18:05:10 | TITLE_1762951489 | IMAGE_1201290847 | 31-08-2003 16:56:01 || 955209697 | 25-12-2003 00:55:43 | TITLE_1064749344 | IMAGE_1335865507 | 11-11-2003 18:37:07 || 1657429856 | 19-12-2003 01:03:13 | TITLE_1931852743 | IMAGE_905288424 | 26-09-2003 07:20:01 || 759381001 | 12-11-2003 10:46:52 | TITLE_878255772 | IMAGE_2014780795 | 30-09-2003 06:54:40 || 1269478951 | 06-11-2003 02:06:22 | TITLE_2026098781 | IMAGE_982272966 | 2003-07-28 11:59:10 || 1049672131 | 04-10-2003 20:55:34 | TITLE_2043080215 | IMAGE_987859662 | 19-11-2003 05:26:37 || 1429108729 | 16-09-2003 19:07:52 | TITLE_424483080 | IMAGE_35379150 | 18-11-2003 22:17:55 || 1672198676 | 13-09-2003 11:49:52 | TITLE_1131552745 | IMAGE_875049630 | 08-06-2003 00:42:58 || 1645878842 | 24-08-2003 13:42:04 | TITLE_1077302833 | IMAGE_702269538 | 02-04-2002 01:21:49 || 172347180 | 2003-08-21 14:26:37 | TITLE_558691044 | IMAGE_1091183587 | 13-08-2003 08:30:22 || 1137674509 | 2003-08-15 08:44:37 | TITLE_1982979709 | IMAGE_1234487941 | 17-12-2003 11:53:28 || 282998112 | 05-08-2003 10:01:34 | TITLE_353831568 | IMAGE_738487608 | 08-11-2003 22:03:22 || 246145344 | 02-08-2003 00:42:31 | TITLE_376954044 | IMAGE_1279375459 | 05-12-2003 02:30:49 || 218409162 | 14-07-2003 02:55:16 | TITLE_1932540991 | IMAGE_1078689211 | 14-07-2003 15:59:37 || 593263087 | 12-07-2003 22:47:01 | TITLE_1604012533 | IMAGE_834822870 | 02-09-2003 05:48:22 || 2115914174 | 06-07-2003 03:06:31 | TITLE_1268165545 | IMAGE_1068632322 | 2003-04-28 16:29:01 || 552557275 | 01-07-2003 16:45:22 | TITLE_2022112717 | IMAGE_1410588295 | 01-11-2003 01:55:16 || 1500437041 | 29-06-2003 20:05:19 | TITLE_35559258 | IMAGE_159953586 | 2003-08-02 10:34:07 || 1098371257 | 09-06-2003 07:29:37 | TITLE_1694076415 | IMAGE_1409619391 | 07-01-2004 01:00:13 || 1570373503 | 22-05-2003 16:45:04 | TITLE_125157894 | IMAGE_723393492 | 26-09-2003 23:22:43 || 1330507411 | 05-05-2003 21:40:07 | TITLE_1571250589 | IMAGE_701840418 | 19-11-2003 20:57:31 || 1666035620 | 26-04-2003 02:51:40 | TITLE_504713706 | IMAGE_1410357553 | 18-11-2003 01:30:04 || 1458179791 | 2003-04-19 05:34:25 | | | || 1365758305 | 2003-03-28 10:09:58 | TITLE_704554170 | IMAGE_2085080137 | 11-11-2003 16:35:19 || 2131082774 | 26-03-2003 16:43:25 | TITLE_1411034929 | IMAGE_303539208 | 14-05-2003 12:59:37 || 103396632 | 2003-02-14 09:11:28 | TITLE_915927396 | IMAGE_1381045723 | 2003-12-28 18:26:28 || 396479202 | 01-02-2003 15:51:40 | | | || 2019916250 | 30-01-2003 00:44:46 | | | || 431091906 | 29-01-2003 13:08:37 | | | || 705166549 | 23-01-2003 21:37:07 | TITLE_1530318643 | IMAGE_257673696 | 2003-08-23 19:06:22 || 1278327049 | 31-12-2002 16:39:40 | TITLE_772845324 | IMAGE_1355754913 | 22-12-2003 16:40:25 || 1871174528 | 30-12-2002 19:03:40 | TITLE_65725764 | IMAGE_522904938 | 06-09-2003 07:08:01 || 611892727 | 22-12-2002 10:19:07 | TITLE_333758274 | IMAGE_734815032 | 22-09-2003 19:16:43 || 758497849 | 04-12-2002 15:05:10 | TITLE_129140574 | IMAGE_244407066 | 15-07-2003 12:38:37 || 518111034 | 17-10-2002 16:38:10 | TITLE_1976498683 | IMAGE_2008599775 | 06-01-2004 16:37:34 || 1737725786 | 15-10-2002 23:52:52 | TITLE_1441053871 | IMAGE_1595265847 | 2003-11-24 15:23:10 || 1206055789 | 03-10-2002 22:07:52 | TITLE_562697952 | IMAGE_198940092 | 2003-08-23 11:56:46 || 702790153 | 31-08-2002 17:37:16 | TITLE_1788304903 | IMAGE_1212944101 | 2003-12-19 22:56:01 |+-------------+--------------------------+--- ---------------+-------------------+--------------- ------+40 rækker i sæt (0,01 sek.)
Her er den nye forespørgsel
VÆLG AAA.author_id, AAA.date_created, IFNULL(BBB.title,'') titel, IFNULL(CCC.filename,'') filnavn, IFNULL(CCC.dato_added) ,'') image_date FROM ( SELECT AA.id, AA.date_added, BB.author_id, BB.date_created FROM ( SELECT A.id,IFNULL(MAX(B.date_added),'1900-01 -01 00:00:00') date_added FROM (SELECT id FROM articles ORDER BY date_created DESC LIMIT 40) A LEFT JOIN article_images B ON A.id =B.article_id GROUP BY A.id ) AA INNER JOIN artikler BB BRUG AF (id ) ) AAA LEFT JOIN article_contents BBB ON AAA.id=BBB.article_id LEFT JOIN article_images CCC ON (AAA.id=CCC.article_id AND AAA.date_added=CCC.date_added) BESTIL EFTER AAA.date_code_created DESC;
Min forespørgsel gør én ting, ingen andres forespørgsel gør:
Min forespørgsel henter de sidste 40 artikler først, før den slutter sig til en anden tabel.
Alle andres forespørgsler forbinder alt først og forsøger derefter at udføre LIMIT 40 på en stor midlertidig tabel.
Min forespørgsels køretid er ikke en faktor for mængden af data.
Uanset prøvestørrelsen, jeg opretter, skal min forespørgsel være den hurtigste !!!