У меня есть некоторые проблемы со следующим кодом в моем приложении PHP / MySQL.Он работает хорошо и занимает около 3-4 секунд, но первое выполнение (за сеанс) занимает около 2 минуты .Я думаю, потому что есть некоторые механизмы автоматического кэширования.Есть способ ускорить первое выполнение?У меня есть root-доступ на этом сервере MySQL, но я не могу изменить структуру БД.
Приложение отображается здесь http://hotel.crosstourpoint.eu/, и медленный сценарий: http://hotel.crosstourpoint.eu/ajax/html_hotel_details.php. Чтобы проверить это, найдите что-то в главном окне.Пример: введите «Milano» и нажмите «Cerca», нажмите на опцию «Milano», выберите даты начала и окончания («Giorno di Arrivo - Giorno di partenza») и снова нажмите «Cerca».Значок Info (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 hostname
2.6.24-23-server # 1 SMP Ср 1 апреля 22:14:30 UTC 2009 x86_64 GNU / Linux
mysql Ver 14.12 Distrib5.0.51a, для debian-linux-gnu (x86_64) с использованием readline 5.2
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