优化周一1-9周期的表格结构

时间:2013-02-16 00:57:30

标签: php mysql

优化下表的最佳方法是什么?

// Monday period 1

echo '
<td class="gridSide">Mon</td>
<td class="box" id="grid2_mon1">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 1
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 2

echo '
<td class="box" id="grid2_mon2">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 2
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 3

echo '
<td class="box" id="grid2_mon3">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 3
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 4

echo 
'<td class="box" id="grid2_mon4">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 4
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 5

echo '
<td class="box" id="grid2_mon5">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 5
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 6

echo '
<td class="box" id="grid2_mon6">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 6
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 7

echo '
<td class="box" id="grid2_mon7">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 7
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 8

echo '
<td class="box" id="grid2_mon8">'; 
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 2
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';

// Monday period 9

echo '
<td class="box" id="grid2_mon9">';
$sql = "
SELECT 
  COUNT(*) totalCount 
FROM 
  ts_room 
WHERE 
  NOT EXISTS (
    SELECT 1
    FROM ts_roompref 
      JOIN ts_request 
      ON ts_roompref.request_id = ts_request.roompref_id
      AND day_id = 1 
      AND period_id = 2
    WHERE 
      ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
}

1 个答案:

答案 0 :(得分:2)

主要需要索引用于选择行的列 - 在ON和WHERE子句中。使用多列时使用复合索引,通常将最低基数列放在第一位。

通常可以通过将SELECT子句中的列放在索引中来获得额外的性能提升,从而允许MySQL从索引中读取所需的所有内容,而无需从磁盘读取基础记录。

哦,无论何时重复相同的查询,都要考虑重构一些事情。