Skip to content

Instantly share code, notes, and snippets.

@pierre-dargham
Created February 10, 2022 15:18
Show Gist options
  • Save pierre-dargham/97745f3aaf7e0f04bd6b569c6fe6af94 to your computer and use it in GitHub Desktop.
Save pierre-dargham/97745f3aaf7e0f04bd6b569c6fe6af94 to your computer and use it in GitHub Desktop.

Page avec N champs ACF relationnels

Chargemement SQL par défaut :

Les requêtes sont déclanchées lors des get_field, en cours de templating :

get_field 1 -> post 1

  • 1 requete = fetch post : SELECT * FROM wp_posts WHERE ID IN (1)
  • 1 requete = fetch post meta : SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1)
  • 1 requete = fetch post terms & taxonomies : SELECT t.*, tt.*, tr.object_id FROM wp_terms (...) WHERE tr.object_id IN (1)

get_field 2 -> post 2

  • 1 requete = fetch post : `SELECT * FROM wp_posts WHERE ID IN (2)
  • 1 requete = fetch post meta : SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (2)
  • 1 requete = fetch post terms & taxonomies : SELECT t.*, tt.*, tr.object_id FROM wp_terms (...) WHERE tr.object_id IN (2)

get_field 3 -> post 3

  • 1 requete = fetch post : SELECT * FROM wp_posts WHERE ID IN (3)
  • 1 requete = fetch post meta : SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (3)
  • 1 requete = fetch post terms & taxonomies : SELECT t.*, tt.*, tr.object_id FROM wp_terms (...) WHERE tr.object_id IN (3)

(...)

get_field N -> post N

  • 1 requete = fetch post : `SELECT * FROM wp_posts WHERE ID IN (N)
  • 1 requete = fetch post meta : SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (N)
  • 1 requete = fetch post terms & taxonomies : SELECT t.*, tt.*, tr.object_id FROM wp_terms (...) WHERE tr.object_id IN (N)

Chargemement avec prefetch SQL des relations ACF :

Lors du bootstrap WP, en amont du templating :

On récupère l'ensemble des ID des relations ACF du post courant

Puis on fetch l'ensemble de ces objets d'un coup :

  • 1 requete = fetch post : SELECT * FROM wp_posts WHERE ID IN (1, 2, 3 ... N)
  • 1 requete = fetch post meta : SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (1, 2, 3 ... N)
  • 1 requete = fetch post terms & taxonomies : SELECT t.*, tt.*, tr.object_id FROM wp_terms (...) WHERE tr.object_id IN (1, 2, 3 ... N)

Lors des get_field / utilisation dans le templating, l'ensemble des posts sont dans le cache interne de WP, aucune requête supplémentaire n'est nécessaire

COMPARAISON :

Mode 1 :

TOTAL = 3*N requêtes, non scalable

  • Pour N = 1 -> 3 requêtes
  • Pour N = 5 -> 15 requêtes
  • Pour N = 20 -> 60 requêtes

Mode 2 :

TOTAL = 3 requêtes, scalable

  • Pour N = 1 -> 3 requêtes
  • Pour N = 5 -> 3 requêtes
  • Pour N = 20 -> 3 requêtes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment