Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Performance von INNER JOIN Abfrage erhöhen - Vorschläge?

Empfohlene Antworten

Veröffentlicht

Hallo Boardies,

ich habe ein WP-Plugin geschrieben. Eine der Abfragen ist ein INNER JOIN. Die Abfrage erstellt eine generische ID, weil ich mit GROUP BY das Problem nicht lösen konnte. Jetzt braucht diese eine Abfrage jedoch 0,2 sec auf meinem Root und auf normalen Hostingpaketen wahrscheinlich mehr Zeit.

Hat jemand von Euch eine Idee, wie ich die Abfrage beschleunigen kann?


SELECT a.post_id AS post_id, a.ref AS title, CONCAT( ' (', a.hits, ')' ) AS note

FROM wp_fire_refs AS a

INNER JOIN (

SELECT CONCAT( post_id, '_', MAX( hits ) ) AS posthits

FROM wp_fire_refs

WHERE issearchengine =1

AND fire_date > '2009-01-01'

GROUP BY post_id

ORDER BY posthits DESC

) AS sub ON ( CONCAT( a.post_id, '_', a.hits ) = sub.posthits )

WHERE issearchengine =1

AND a.fire_date > '2009-01-01'

GROUP BY a.post_id

ORDER BY a.hits DESC

LIMIT 0 , 10;

Grüße Timbob

lass den EXPLAIN PLAN für die abfrage laufen,

setz einen INDEX auf zumindest wp_fire_refs.post_id,

lass den EXPLAIN PLAN nochmals laufen.

dann sollten unterschiede - selbst bei MySQL (ist es das?) bemerkbar sein.

s'Amstel

hmm, CONCAT von Strings als ON-Vergleich für den inner join und davor aber noch nen Sub-Select. Das is schon heftig :)

Bist Du sicher, dass Du den ORDER BY im Sub-Select brauchst?

Wenn die Felder im Where-Statement als Sekundär-Index vorhanden sind, sollte er den nehmen.

Hab nun einen Teil des generischen Vergleich ausgelagert in eine weitere Spalte und ORDER BY im INNER JOIN entfernt:

SELECT a.post_id AS post_id, a.ref AS title, hits AS note, gen_id AS posthits

FROM wpneu_keywordfire_refs AS a

INNER JOIN (

SELECT CONCAT( post_id, '_', MAX( hits ) ) AS posthits

FROM wpneu_keywordfire_refs

WHERE issearchengine =1

AND keywordfire_date > '2009-01-01'

GROUP BY post_id

) AS sub ON ( posthits = sub.posthits )

WHERE issearchengine =1

AND a.keywordfire_date > '2009-01-01'

GROUP BY a.post_id

ORDER BY a.hits DESC

LIMIT 0 , 10;
@Amstelchen Ein EXPLAIN zeigt nun:
id 	select_type 	table 	type 	possible_keys 	key 	key_len 	ref 	rows 	Extra

1 	PRIMARY 	a 	ref 	date,is 	is 	1 	const 	44 	Using where; Using temporary; Using filesort

1 	PRIMARY 	<derived2> 	ALL 	NULL 	NULL 	NULL 	NULL 	25 	Using where

2 	DERIVED 	wpneu_keywordfire_refs 	ref 	date,is 	is 	1 	  	44 	Using where; Using temporary; Using filesor

Insgesamt scheinen alle Anfragen (auch simple), die das Plugin betreffen langsam zu sein. Muss ich irgend etwas besonderes beachten bzgl. Feldern, Indizies und deren Nutzung?

So eine Arbeit und am Ende läuft das Ganze nicht, weil es zu langsam ist bzw. die Server zu stark beansprucht. Klasse :upps

Habt Ihr noch einen Tipp?

Gruß Tim

Für alle die das Ganze interessiert hier die letzte Version des SQL-Satzes:

SELECT a.post_id AS post_id, a.ref AS title, CONCAT(' (', a.hits, ')') AS note

FROM keywordfire_refsAS a


INNER JOIN (

SELECT post_id, Max(hits) as maxhits

FROM keywordfire_refs

WHERE (issearchengine =1) AND (keywordfire_date > '$date') AND (post_id > 0) AND (hits>1)

GROUP BY post_id ) AS sub

 ON ( (a.post_id = sub.post_id) AND (a.hits=sub.maxhits)

)

WHERE (issearchengine =1) AND (a.keywordfire_date > '$date') AND (a.post_id > 0) AND (a.hits>1)

GROUP BY a.post_id ORDER BY a.hits DESC LIMIT 0 , $maxresults";

Dieser Satz ist nun im Faktor 3 bis 4 schneller.

Der Tipp mit Explain und den Indizies war hilfreich, aber konnte zur Geschwindigkeitserhöhung leider nicht beitragen.

Gruß Tim

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.