dimanche 5 juillet 2015

Pick 10 random rows in mysql the most effective way in large table

i have a large table and i want to pick 10 random rows in the fast way.

rows are delete sometimes, this means i cant random between minId - maxId.

i want different rows without some order.

i tried some codes that i saw in google but its not helped me. now i using in this code:

        SELECT `services`.*
        FROM `services`
        JOIN(
            SELECT `serviceId` AS `sid`
            FROM `services`
            WHERE `services`.`catId` = '{$row["catId"]}' && `subCatId` IS NULL
            ORDER BY RAND( )
            LIMIT 10
        ) tmp
        LEFT JOIN `cats` ON `services`.`catId` = `cats`.`catId`
        WHERE `services`.`catId` = '{$row["catId"]}' && `subCatId` IS NULL && `services`.`serviceId` = `tmp`.`sid`
        GROUP BY `services`.`serviceId`

but the page load slowly

thank a lot




Aucun commentaire:

Enregistrer un commentaire