パーマリンク設定すると大量のクエリで遅い
-
製作中のページでベンチを取ると初めから大量のクエリを発行していたため、調べるとrewrite.phpのWP_Rewriteクラス内のpage_uri_indexメソッドで大量のクエリを発行しているのが分かりました。
参考のために速度改善したコードを載せます。
get_page_uriとget_page_hierarchyメソッドを追加していますが、元々post.php内で関数で実装され、他でも使用されているために、他に影響を及ぼさないようにクラス関数としました。
まだ、あまり検証していませんが、何かのお役に立てれば幸いです。
他力本願ですが、出来れば、本家に、もうちょい考えたコードを作成してマージしてくれる人がいたらと思い投稿します(汗
WP本体改造なので、VerUPした時、面倒ですし。。。
function page_uri_index() { global $wpdb; //get pages in order of hierarchy, i.e. children after parents $posts = $wpdb->get_results("SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'page'"); $posts = $this->get_page_hierarchy($posts); //now reverse it, because we need parents after children for rewrite rules to work properly $posts = array_reverse($posts, true); $page_uris = array(); $page_attachment_uris = array(); if ( !$posts ) return array( array(), array() ); foreach ($posts as $id => $post) { // URL => page name $uri = $this->get_page_uri($id,$posts); $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id )); if ( $attachments ) { $attachments = $this->get_page_hierarchy($attachments,$id); foreach ( $attachments as $attachment ) { $attach_uri = $this->get_page_uri($attachment->ID); $page_attachment_uris[$attach_uri] = $attachment->ID; } } $page_uris[$uri] = $id; } return array( $page_uris, $page_attachment_uris ); } function get_page_uri($page_id,&$posts) { $page = $posts[$page_id]; $uri = $page->post_name; // A page cannot be it's own parent. if ( $page->post_parent == $page->ID ) return $uri; while ($page->post_parent != 0) { $page = $posts[$page->post_parent]; $uri = $page->post_name . "/" . $uri; } return $uri; } function get_page_hierarchy(&$posts, $parent = 0) { $result = array ( ); if ($posts) { foreach ( (array) $posts as $post) { if ($post->post_parent == $parent) { $result[$post->ID] = $post; $children = $this->get_page_hierarchy($posts, $post->ID); $result += $children; //append $children to $result } } } return $result; }
P.S.
昔からこんなコードでしたっけ。。。?
って自分で調べろって??すいません、貧乏暇なしです。。。(汗
アタッチメントは基本使わないんだけどなぁ。。。画像投稿でもループするのはきつい。。。
2件の返信を表示中 - 1 - 2件目 (全2件中)
2件の返信を表示中 - 1 - 2件目 (全2件中)
- トピック「パーマリンク設定すると大量のクエリで遅い」には新たに返信することはできません。