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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建