Don haɓaka pagination a cikin MySQL, zaku iya amfani da dabaru masu zuwa:
Yi amfani da kalmomi LIMIT
da OFFSET
kalmomi
Yi amfani da LIMIT
juzu'in don iyakance adadin sakamakon da aka dawo kowane shafi kuma amfani da shi OFFSET
don tantance matsayin farawa na sakamakon shafi na gaba.
SELECT * FROM products LIMIT 10 OFFSET 20;
A cikin misalin da ke sama, tambayar ta dawo da sakamako 10 tun daga matsayi na 20.
Yi amfani da fihirisa don filayen da aka yi amfani da su a cikin fage
Ƙirƙiri fihirisa don filayen da aka yi amfani da su a cikin ORDER BY
jumlar WHERE
tambayar rubutun. Wannan yana taimakawa MySQL
bincika da daidaita bayanan cikin sauri.
CREATE INDEX idx_created_at ON products(created_at);
Saita ƙwaƙwalwar ajiya cache
Sanya ƙwaƙwalwar MySQL cache
don adana tambayoyin da aka rubuta da bayanan da aka samu kwanan nan. Wannan yana rage lokacin samun faifai kuma yana inganta saurin tambaya.
[mysqld]
...
query_cache_type = 1
query_cache_size = 1G
Yi amfani Paginated Query Cache
da fasaha
Don adana sakamakon tambayoyin shafi, zaku iya amfani da caches na ƙwaƙwalwar ajiya kamar Redis ko Memcached. Lokacin da aka aiwatar da tambayar pagination, ana adana sakamakon a cikin ma'ajiyar, kuma tambayoyin na gaba zasu iya sake amfani da sakamakon daga ma'ajiyar maimakon sake aiwatar da tambayar. Wannan yana rage nauyin bayanai kuma yana inganta saurin fage.
Yi amfani da dabarun inganta tambaya
Yi amfani EXPLAIN
don tantancewa da haɓaka tambayoyin shafi. Bincika shirin aiwatar da tambaya kuma tabbatar da cewa ana amfani da fihirisa da yanayin bincike yadda ya kamata.
Inganta tsarin bayanai
Yi la'akari da yadda kuke ƙira da tsara tsarin bayanan ku don tabbatar da ya yi daidai da buƙatun rubutun ku. Wannan na iya haɗawa da yin amfani da ƙaramin tebur ko wasu dabaru don haɓaka maido da bayanai don ɓarna.
Ka tuna cewa inganta pagination tsari ne mai rikitarwa wanda ke buƙatar cikakken gwaji da kimantawa. Tabbatar cewa kun tantance tasirin canje-canje kuma ku inganta gwargwadon buƙatu da yanayin da kuke aiki da su.