私のCMSでは、すべてが独立しているため冗長クエリを作成することがあります。このようなクエリをキャッシュしてもよろしいですか?:"キャッシュ" mySQLクエリ、これはOKのプラクティスですか?
global $cache_query, $cache_result;
if(!is_array($cache_query) || !is_array($cache_result) || !in_array($q,$cache_query))
{//The query is new.
$r = query_($q);
$cache_query[] = $q;
$cache_result[] = $r;
}
else
{//The query is cached.
foreach($cache_query as $k=>$c) // Cycle through my cached queries to find the key. Is there an getElementKey function?
if($c == $q) {
if(@mysql_fetch_array($cache_result[$k])) // is the resource is still valid?
mysql_data_seek($cache_result[$k], 0); // rewind the pointer
else
$cache_result[$k] = query_($q); // reset the resource
$r = $cache_result[$k];
break;
}
}
return $r;
ORは新しいクエリを毎回作成する方がよいでしょうか? $q
が私のクエリです。関数query_()
は何か問題が生じた場合にリソースとログを返します。
私はインデックスが長すぎることを心配していました。クエリ内に ''がある場合 – Jason
+1提案/改善:SQLクエリがかなり大きくなる可能性があるため、クエリ自体をassoc配列のキーとして使用するのではなく、SQLクエリのハッシュ値を計算できます – Ronnis
@ロンニス、私はこれが好きです。良い考え。 – Jason