我有搜索過濾器。每個過濾器/屬性都有我需要顯示的自己的值。大量非常簡單的sql查詢是否會影響性能?
<?php foreach(getAdditionalFilters() as $attributeName => $filterData): ?>
<ul>
<?php showLiCheckBoxes($attributeName); ?>
</ul>
<?php endforeach; ?>
有兩種方法如何顯示過濾器:
1.
function showLiCheckBoxes($attribute,$checked=null){
$CI = get_instance();
$CI->load->model('helper_model');
$allAttrOptions = $CI->helper_model->getAttrOptions($attribute);
foreach($allAttrOptions as $key => $option){
//print html of inputs like <li><input type='checkox' value='{$option['optionId']}...
};
}
function getAttrOptions($attrCode){
$sql = "SELECT option_id as optionId, label FROM eav_attribute_option
INNER JOIN eav_attribute USING(attr_id)
WHERE code='$attrCode'";
$query = $this->db->prepare($sql);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
return $query->fetchAll();
}
2.
function showLiCheckBoxes($attribute,$checked=null){
foreach(Attributes::${$attribute} as $key => $value){
//print html of inputs like <li><input type='checkox' value='{$option['optionId']}...
};
}
class Attributes
{
public static $education = array(0 => 'No answer',
1 => 'High school',
2 => 'Some college',
3 => 'In college',
4 => 'College graduate',
5 => 'Grad/professional school',
6 => 'Post grad');
public static $...
所以在第一種方式的屬性都存儲在數據庫選項和正如你所看到的每個屬性額外的查詢所做的。在第二種方式中,屬性選項存儲在數組中。
我不是問哪個方法是正確的,因爲還有一些其他的事實沒有寫在這裏。我所要求的僅僅是如果只有500行的表eav_attribute_option的20個額外非常簡單的查詢可能會對性能產生影響。我應該關心它還是我寫在這裏的查詢非常簡單,而且表格太小以致根本沒有任何區別?
當然,我也可以用1查詢,但這不是問題再次。我問自己,如果這麼多額外的請求到SQL服務器可能是一件壞事,而不是查詢本身。
嗯。 'FROM eav_attribute_option'表明您可能正在使用EAV結構。你是? –
是的,我正在考慮。 – user1324762