我的 PHP/MySQL 应用程序中的以下代码存在一些问题。它运行良好,大约需要 3-4 秒,但第一次执行(每个会话)大约需要2 分钟。我认为是因为有一些自动缓存机制。有没有一种方法可以加快第一次执行?我在这个 MySQL 服务器上获得了 root 访问权限,但我无法修改数据库结构。
该应用程序在此处可见http://hotel.crosstourpoint.eu/,慢速脚本是http://hotel.crosstourpoint.eu/ajax/html_hotel_details.php。要查看它,请在主框中搜索一些内容。示例:输入“Milano”并单击“Cerca”,单击选项“Milano”,选择开始日期和结束日期(“Giorno di arrivo - Giorno di partenza”)并再次单击“Cerca”。信息 (I) 图标打开带有 ajax 调用的慢速脚本。
谢谢。
代码
<?php
// open mysqli connection
$mysqli = new mysqli('localhost', 'hotelbeds', 'import', 'hotelbeds');
if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }
$code = (int) $_REQUEST['code'];
$h = array();
// hotel position
$request = '
SELECT
NAME, LATITUDE, LONGITUDE
FROM
HOTELS
WHERE
HOTELCODE = ' . $code . ' ';
$stmt = $mysqli->prepare($request);
$stmt->execute();
$stmt->bind_result( $h['name'], $h['latitude'], $h['longitude'] );
$stmt->fetch();
$stmt->close();
unset($stmt);
unset($request);
// loading descriptions
$request = '
SELECT
HotelFacilities, HotelHowToGetThere, HotelComments
FROM
HOTEL_DESCRIPTIONS
WHERE
HotelCode = ' . $code . '
AND
LanguageCode = "' . HB_LANGCODE . '" ';
$stmt = $mysqli->prepare($request);
$stmt->execute();
$stmt->bind_result( $h['facilities'], $h['hotelhowtogetthere'], $h['comments'] );
$stmt->fetch();
$stmt->close();
unset($stmt);
unset($request);
// hotel images
$request = '
SELECT
IMAGEPATH
FROM
HOTEL_IMAGES
WHERE
HOTELCODE = ' . $code . ' ';
$stmt = $mysqli->prepare($request);
$stmt->execute();
$stmt->bind_result( $imagepath );
$images = array();
while( $stmt->fetch() ) array_push( $images, $imagepath );
$stmt->close();
unset($stmt);
unset($request);
表结构
酒店:约 50.000 行
HOTELS_DESCRIPTIONS 约 600.000 行
HOTELS_IMAGES:大约 180.000 行
CREATE TABLE `HOTELS` (
`HOTELCODE` varchar(8) collate utf8_spanish_ci NOT NULL,
`NAME` varchar(50) collate utf8_spanish_ci NOT NULL,
`CATEGORYCODE` varchar(5) collate utf8_spanish_ci NOT NULL,
`DESTINATIONCODE` varchar(3) collate utf8_spanish_ci NOT NULL,
`ZONECODE` varchar(8) collate utf8_spanish_ci default NULL,
`CHAINCODE` varchar(5) collate utf8_spanish_ci default NULL,
`LICENCE` varchar(15) collate utf8_spanish_ci default NULL,
`LATITUDE` varchar(45) collate utf8_spanish_ci default NULL,
`LONGITUDE` varchar(45) collate utf8_spanish_ci default NULL,
PRIMARY KEY (`HOTELCODE`),
KEY `HOTELS_CATEGORIES_FK` (`CATEGORYCODE`),
KEY `HOTELS_ZONES_FK` (`ZONECODE`),
CONSTRAINT `HOTELS_ZONES_FK` FOREIGN KEY (`ZONECODE`) REFERENCES `ZONES` (`ZONECODE`) ON DELETE CASCADE,
CONSTRAINT `HOTELS_CATEGORIES_FK` FOREIGN KEY (`CATEGORYCODE`) REFERENCES `CATEGORIES` (`CategoryCode`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci COMMENT='Hotels'
CREATE TABLE `HOTEL_DESCRIPTIONS` (
`HotelCode` varchar(8) collate utf8_spanish_ci NOT NULL,
`LanguageCode` varchar(3) collate utf8_spanish_ci NOT NULL,
`HotelFacilities` varchar(2000) collate utf8_spanish_ci default NULL,
`HotelLocationDescription` varchar(2000) collate utf8_spanish_ci default NULL,
`HotelRoomDescription` varchar(2000) collate utf8_spanish_ci default NULL,
`HolelSportDescription` varchar(2000) collate utf8_spanish_ci default NULL,
`HotelMealsDescription` varchar(2000) collate utf8_spanish_ci default NULL,
`HotelPaymentMethods` varchar(2000) collate utf8_spanish_ci default NULL,
`HotelHowToGetThere` varchar(2000) collate utf8_spanish_ci default NULL,
`HotelComments` varchar(2000) collate utf8_spanish_ci default NULL,
PRIMARY KEY (`HotelCode`,`LanguageCode`),
KEY `HOTEL_DESCRIPTIOS_LANGUAGES_FK` (`LanguageCode`),
CONSTRAINT `HOTEL_DESCRIPTIOS_LANGUAGES_FK` FOREIGN KEY (`LanguageCode`) REFERENCES `LANGUAGES` (`LANGUAGECODE`) ON DELETE CASCADE,
CONSTRAINT `HOTEL_DESCRIPTIOS_HOTELS_FK` FOREIGN KEY (`HotelCode`) REFERENCES `HOTELS` (`HOTELCODE`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci COMMENT='Hotel_Descriptions'
CREATE TABLE `HOTEL_IMAGES` (
`HOTELCODE` varchar(8) collate utf8_spanish_ci NOT NULL,
`IMAGECODE` varchar(3) collate utf8_spanish_ci NOT NULL,
`ORDER_` varchar(5) collate utf8_spanish_ci NOT NULL,
`VISUALIZATIONORDER` varchar(5) collate utf8_spanish_ci default NULL,
`IMAGEPATH` varchar(2000) collate utf8_spanish_ci NOT NULL,
PRIMARY KEY (`HOTELCODE`,`IMAGECODE`,`ORDER_`),
KEY `HOTEL_IMAGES_IMAGE_TYPES_FK` (`IMAGECODE`),
CONSTRAINT `HOTEL_IMAGES_IMAGE_TYPES_FK` FOREIGN KEY (`IMAGECODE`) REFERENCES `IMAGE_TYPES` (`IMAGECODE`) ON DELETE CASCADE,
CONSTRAINT `HOTEL_IMAGES_HOTELS_FK` FOREIGN KEY (`HOTELCODE`) REFERENCES `HOTELS` (`HOTELCODE`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci COMMENT='Hotels_Images'
附加信息(编辑):
Ubuntu 64bit 8.04.2 Linux 主机名
2.6.24-23-server #1 SMP Wed Apr 1 22:14:30 UTC 2009 x86_64 GNU/Linux
mysql Ver 14.12 Distrib 5.0.51a,适用于使用 readline 5.2 的 debian-linux-gnu (x86_64)
innodb_buffer_pool_size 512
解释:
>> EXPLAIN SELECT NAME, LATITUDE, LONGITUDE FROM HOTELS WHERE HOTELCODE = 136224
id | select_type | table | type | possibile_keys | key key_len | ref | rows
1 SIMPLE HOTELS ALL PRIMARY 47373 Using where
>> EXPLAIN SELECT HotelFacilities, HotelHowToGetThere, HotelComments FROM HOTEL_DESCRIPTIONS WHERE HotelCode = 136224 AND LanguageCode = "ITA"
id | select_type | table | type | possibile_keys | key key_len | ref | rows
1 SIMPLE HOTEL_DESCRIPTIONS ref PRIMARY,HOTEL_DESCRIPTIOS_LANGUAGES_FK HOTEL_DESCRIPTIOS_LANGUAGES_FK 11 const 75378 Using where
>> EXPLAIN SELECT IMAGEPATH FROM HOTEL_IMAGES WHERE HOTELCODE = 136224
id | select_type | table | type | possibile_keys | key key_len | ref | rows
1 SIMPLE HOTEL_IMAGES ALL PRIMARY 158786 Using where