[mysql] snelheid van limit

Pagina: 1
Acties:

  • HenkS
  • Registratie: Mei 2000
  • Laatst online: 21:01

HenkS

Da_king alias HenkS

Topicstarter
heb ff een vraag:

ik vind limit echt super handig bij mysql, zeker bij het genereren van overzichten over meerderde pagina's

MAAR, ik hoor ook vaak dat het de snelheid verhoogt, maar dit snap ik niet, want stel je hebt limit 40,30 ofzo.

dan moet mysql, toch eerst echt alles ophalen, voordat mysql weet welke stuk 40,30 is??? dus hoe kan LIMIT dan in vredesnaam de snelheid bevorderen?

offtopic:
vind trouwens dat niveau van topics weer wat omhoog gaat hier, erg fijn om te zien :) weer eens leerzaam :Y)

  • chem
  • Registratie: Oktober 2000
  • Laatst online: 27-08 13:53

chem

Reist de wereld rond

een limit is idd niet sneller dan de range die je opgeeft, en dat zorgt ervoor dat het soms helemaal niks uitmaakt.

bij een
code:
1
select * from table limit 0,10

zal mysql sneller zijn dan de volledige select;
bij
code:
1
select * from table order by column desc limit 0,10

zal mysql toch echt eerst de top 10 moeten bepalen. Dat betekende dat de complete recordset moet worden gesorteerd.

Het spreekt voor zich dat indices hier weer een grote rol in kunnen spelen: als de query geheel in indices kan worden 'opgelost', hoeft mysql alleen nog maar DAAR de limit op uit te voeren en de records uit de db te rukken (dat is dan nog slechts een formaliteit)

zie ook http://www.mysql.com/doc/L/I/LIMIT_optimisation.html

Klaar voor een nieuwe uitdaging.


  • chem
  • Registratie: Oktober 2000
  • Laatst online: 27-08 13:53

chem

Reist de wereld rond

kleine correctie: mysql hoeft de db niet volledige te sorteren bij een order by ... limit ..,...:
In some cases MySQL will handle the query differently when you are using LIMIT # and not using HAVING:
  • If you are selecting only a few rows with LIMIT, MySQL will use indexes in some cases when it normally would prefer to do a full table scan.
  • If you use LIMIT # with ORDER BY, MySQL will end the sorting as soon as it has found the first # lines instead of sorting the whole table.
  • When combining LIMIT # with DISTINCT, MySQL will stop as soon as it finds # unique rows.
  • In some cases a GROUP BY can be resolved by reading the key in order (or do a sort on the key) and then calculate summaries until the key value changes. In this case LIMIT # will not calculate any unnecessary GROUP BY's.
  • As soon as MySQL has sent the first # rows to the client, it will abort the query (If you are not using SQL_CALC_FOUND_ROWS).
  • LIMIT 0 will always quickly return an empty set. This is useful to check the query and to get the column types of the result columns.
  • The size of temporary tables uses the LIMIT # to calculate how much space is needed to resolve the query.

Klaar voor een nieuwe uitdaging.


  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 09-09 20:58

Janoz

Moderator Devschuur®

!litemod

Bedenk trouwens wel dat het ophalen uit de DB niet alles is. Ook de resultset die uiteindelijk naar je php (of whatever) wordt gestuurd is met limit een stuk kleiner. Stel dat de server een resultset aan zou maken van een paar miljoen records waarvan je er uiteindelijk maar 100 gebruikt, dan is voor die paar miljoen records wel geheugenruimte gereserveerd.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'