dpd20130 2014-12-28 04:35
浏览 49
已采纳

Mysql SELECT多列或一列,然后用php显示结果。 哪个选项更适合性能

Html many (~30) check-boxes like

<input name="heated_seats" value="1" id="heated_seats" type="checkbox">
<input name="electric_windows" value="1" id="electric_windows" type="checkbox">
<input name="cruise_control" value="1" id="cruise_control" type="checkbox">

User may check any of them (may check all, may check nothing).

Thinking about performance. Which option would be faster and use less resources?

Option 1

mysql

HeatedSeats | ElectricWindows | CruiseControl
----------------------------------------------
   1        |     1           |   0
   1        |     0           |   0
   0        |     0           |   0

Query like SELECT HeatedSeats, ElectricWindows, CruiseControl .....

Php like if( HeatedSeats == 1 ){ echo 'HeatedSeats'; } Many if

Option 2

mysql

OneColumnWithPossiblyLongVarcharOrText
----------------------------------------------
Heated seats, Electric windows, ''
Heated seats, '', ''
'', '', ''

Query like SELECT OneColumnWithPossiblyLongVarcharOrText .....

Php like

 $arr = explode( ',' OneColumnWithPossiblyLongVarcharOrText )
 foreach( $arr as $k => $v ){
 if( strlen($v) > 0 ){ echo $v; }
 }

For example many visitors do the same select on different rows.

Which option takes less time and uses less resources?

Or may be there some better option?

  • 写回答

1条回答 默认 最新

  • dongpu1315 2014-12-28 04:55
    关注

    Option 1 will be better for your scenario and it will take less time if many visitors do the same select on different rows.

    With Option 1

     You have to query and check for multiple conditions which is not good but will help you in future.
     On insert you have to insert multiple checkbox value to multipel columns.
    

    With Option 2

    You have to query and with some exploding and looping you can easily check which option is checked.
    On insert you need to just insert single column value. 
    But on update it is hard to maintain delimeter values
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?