簡體   English   中英

Mysql選擇多列或一列,然后使用php顯示結果。 哪個選項更適合表現

[英]Mysql SELECT multiple columns or one column and then display results with php. Which option better for performance

HTML許多(〜30)復選框

<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">

用戶可以檢查其中的任何一個(可以檢查所有,可以不檢查)。

考慮性能。 哪個選項會更快並且使用更少的資源?

選項1

MySQL的

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

查詢,例如SELECT HeatedSeats, ElectricWindows, CruiseControl .....

if( HeatedSeats == 1 ){ echo 'HeatedSeats'; } if( HeatedSeats == 1 ){ echo 'HeatedSeats'; }很多if

選項2

MySQL的

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

SELECT OneColumnWithPossiblyLongVarcharOrText .....一樣查詢SELECT OneColumnWithPossiblyLongVarcharOrText .....

像php

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

例如,許多訪客在不同的行上執行相同的選擇。

哪個選項花費更少的時間並使用更少的資源?

還是有更好的選擇?

選項1會更適合您的情況,並且如果許多訪問者在不同的行上執行相同的選擇,它將花費更少的時間。

帶有選項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.

帶有選項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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM