iT邦幫忙

0

WordPress 之 WP_Query 查詢 - Mysql time out

不明 2020-10-25 07:26:531069 瀏覽

各位大大好,小弟我發生了問題,我在我的本機上使用XAMPP架開發環境,最近要使用 WordPress 寫擴充兩個功能:
1.文章至頂 - 並且還需要截止日期 (時間到了貼文不會再至頂了)
2.文章下架 - 並且還需要下架日期 (時間到了貼文就不會再出現在文章列表中)

我使用 ACF 外掛開了4個 meta_key 欄位:

  1. news_down_date_bollen --- 是否要設置下架日期 (型態:布林)
  2. news_down_date --- 下架日期 (型態:日期)
  3. news_top --- 是否要至頂 (型態:布林)
  4. news_top_date --- 至頂到截止日 (型態:日期)

長這樣 ▽
https://ithelp.ithome.com.tw/upload/images/20201025/20120807SlbafjiI54.jpg

然後,我使用 WP_Query 在頁面上查詢符合我至頂文章的條件並迴圈顯示文章列表,我的查詢邏輯是這樣:
1.有設置下架日期,並且下架日期超過今天日期 --- 符合條件
2.無設置下架日期 --- 符合條件
上面2個條件一組只需要符合一個條件即可 所以我使用 "OR"

3.有設置至頂日期,並且至頂日期超過今天日期 --- 符合條件
接下來只要把符合 1or2 AND 3 的文章顯示就好

以下是我在WP_Query裡的查詢代碼段

 'meta_query' => array(
    'relation'  => 'AND',
    array( // 檢查下架條件
        'relation'  => 'OR',
        array(
            'relation'  => 'AND',
            array(
                'key' => 'news_down_date_bollen',
                'value' => 1,
                'compare' => '='
            ),
            array(
                'key'       => 'news_down_date',
                'value'     => date('Ymd'),
                'compare'   => '>='
            ),
        ),
        array(
            'key' => 'news_down_date_bollen',
            'value' => 0,
            'compare' => '='
        )
    ),
    array( // 檢查至頂條件
        'relation' => 'AND',
        array(
            'key'     => 'news_top',
            'value'   => 1,
            'compare' => '='
        ),
        array(
            'key'       => 'news_top_date',
            'value'     =>  date('Ymd'),
            'compare'   => '>='

        )
    )
)

沒錯,這樣的邏輯是對的,我也很順利的篩選出我要的文章列表出來。
不過問題來了,似乎是查詢過於複雜,重整網頁時都會有感覺的停頓個零點幾秒然後再顯示文章列表,而且如果我至頂的文章數過多,頁面就會當掉,mysql會把我的CPU用到100%,讓我不得不關掉mysql後重開。

所以我不太知道是不是我寫法有問題還是有甚麼好解決方法嗎?
先在這邊謝謝各位大大耐心看完。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
咖咖拉
iT邦好手 1 級 ‧ 2020-10-25 13:03:42
最佳解答

As long as you insist on using meta_query, your query will be slow no matter what you do.

If you really want this to work, you should create your own (properly-indexed) DB table of this data as opposed to putting them in the _postmeta table. You could then manually query the database using a JOIN of your custom table against the _posts table. I could easily see this new method being thousands of times faster than the method you’re currently using.

希望對你有幫助

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-10-25 09:06:44

試試看
先改成這樣
如果速度有比較快,快到可以接受的話
我再說明細節

注意:news_down_date 改為必填,且預設值為 2099/12/31

'meta_query' => array(
    array( // 檢查下架條件
        'key'       => 'news_down_date',
        'value'     => date('Ymd'),
        'compare'   => '>='
    )
)
不明 檢舉

歹勢 因為meta_key我是用ACF這個視覺化外掛創建的,該外掛沒有給我能設定預設值的地方,所以我可能需要時間研究一下怎麼用另外的方法給這個欄位預設值。

該外掛長這樣

https://ithelp.ithome.com.tw/upload/images/20201025/20120807q5AcYuz969.png

news_down_date 這個欄位我是設定必填沒錯。

請問預設值為 2099/12/31 有甚麼原理在嗎?

必填就可以了,預設值只是讓使用者更方便而已

如果速度有比較快,快到可以接受的話
我再說明細節

如果結果不正確或不夠快
也不用多說什麼

我要發表回答

立即登入回答