MySQL Abfrage optimieren

xtramen01

Erfahrenes Mitglied
Hallo Leute,
ich habe eine MySQL Abfrage die etwas langsam ist. Ca. 2 Sekunden. Nun meine Frage an einen Profi, kann man diese Abfrage optimieren? Bzw. was könnte der Grund sein das die Abfrage so langsam ist? Vielen Dank!


Code:
        select distinct

        p.products_stock_status_id,
        p.unique_id,
        p.products_delivery_status_id,
        p.products_model,
        pd.products_name,
        m.manufacturers_name,
        p.products_quantity,
        p.products_image,
        p.products_weight,
        p.products_id,
        p.manufacturers_id,
        p.products_price,
        p.products_tax_class_id,
      
        IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price,
        IF(s.status, s.specials_new_products_price, p.products_price) as final_price
      
        from
      
        products_description pd,
        products p
      
        left join
      
        manufacturers m
      
        on
      
        p.manufacturers_id = m.manufacturers_id
      
        left join
      
        specials s
      
        on
      
        p.products_id = s.products_id ,
        products_to_categories p2c
      
        where
      
        p.products_status = '1'
      
        and p.products_id = p2c.products_id
        and pd.products_id = p2c.products_id
        and pd.language_id = '2'
        and p.stores_id = '20'
        and p2c.categories_id IN
      
        (13796, 13681, 13959, 13960, 13694, 13695, 13774, 13775,
        13776, 13777, 13963, 13961, 13964, 13962, 13685, 13779,
        13696, 13711, 13712, 13713, 13754, 13755, 13749, 13750,
        13752, 13753, 13768, 13758, 13769, 13770, 13771, 13772,
        13773, 13778, 13793, 13710, 13714, 13715, 13745, 13746,
        13747, 13748, 13751, 13756, 13792, 13697, 13698, 13699,
        13700, 13701, 13702, 13703, 13704, 13705, 13709, 13965,
        13967, 13706, 13707, 13734, 13744, 13716, 13717, 13718,
        13719, 13720, 13721, 13722, 13723, 13724, 13725, 13726,
        13727, 13732, 13733, 13737, 13740, 13743, 13788, 13728,
        13729, 13730, 13731, 13736, 13739, 13780, 13781, 13786,
        13782, 13741, 13763, 13764, 13766, 13767, 13799, 13800,
        13801, 13802, 13803, 13804, 13935, 13805, 13806, 13807,
        13808, 13809, 13810, 13811, 13812, 13813, 13814, 13815,
        13816, 13817, 13818, 13819, 13820, 13821, 13822, 13823,
        13824, 13825, 13826, 13827, 13828, 13829, 13830, 13831,
        13832, 13833, 13834, 13835, 13836, 13837, 13838, 13839,
        13840, 13841, 13842, 13843, 13844, 13845, 13936, 13937,
        13846, 13847, 13848, 13849, 13850, 13851, 13852, 13853,
        13854, 13855, 13856, 13857, 13858, 13859, 13860, 13861,
        13862, 13863, 13864, 13865, 13866, 13867, 13868, 13869,
        13878, 13879, 13880, 13938, 13939, 13881, 13882, 13883,
        13884, 13885, 13886, 13887, 13888, 13889, 13890, 13891,
        13940, 13941, 13942, 13943, 13944, 13972, 13973, 13975,
        13976, 13974, 13977, 13978, 13892, 13893, 13894, 13895,
        13896, 13897, 13898, 13899, 13900, 13901, 13902, 13903,
        13904, 13905, 13906, 13907, 13908, 13909, 13910, 13911,
        13912, 13913, 13914, 13915, 13916, 13917, 13945, 13918,
        13919, 13946, 13947, 13948, 13949, 13950, 13951, 13952,
        13953, 13954, 13955, 13956, 13920, 13971, 13921, 13922,
        13923, 13924, 13925, 13926, 13927, 13928, 13929, 13930,
        13931, 13932, 13933, 13934, 13957, 13958, 13970, 13979,
        13980, 13966)
      
        group by pd.products_name order by final_price asc limit 0, 24
 
Zuletzt bearbeitet:
Ein paar Beobachtungen:
  1. Vermische nicht alte und neue Join Syntax. Mein Tipp: verwende ausschliesslich inner/left/right/.... joins. In diesem Zusammenhang hättest du wahrscheinlich auch sofort festgestellt, dass products_description pd und products p hier gar nicht direkt gejoint werden, sondern nur via products_to_categories p2c, was sehr wahrscheinlich falsch ist.
  2. Ich würde mal vermuten, dass bei diesen Tabellennamen alles inner join sein sollte
  3. p.products_status = '1' und pd.language_id = '2 und p.stores_id = '20'. Sind das wirklich Strings? Wenn nein, dann schreib besser =1, =2 =20
  4. group by brauchst du bei dieser Query gar nicht. Da wird nichts aggregiert
  5. Deine Join Spalten haben einen Index?
  6. Bei so einer langen Liste für p2c.categories_id macht ein Index darauf evtl. auch Sinn.
 
Danke für die Tipps, ich werde es auf jeden Fall durchgehen.
Leider muss das "group by pd.products_name" sein, denn der Produktname kann mehrmals vorkommen...also z.b. ist ein Artikel mit dem selben Namen in verschiedenen Kategorien vorhanden und soll nur einmal angezeigt werden. Gibts da noch einen anderen Weg? Z.b. mit distinct auf eine Spale? geht das?
 
Zurück