我有一张包含数百万条记录的表,目前表的大小为2GB,预计会进一步增长
CREATE TABLE `test` (
`column_1` int(11) NOT NULL AUTO_INCREMENT,
`column_2` int(11) NOT NULL,
`column_3` int(11) NOT NULL,
`column_4` int(11) NOT NULL,
`column_5` datetime NOT NULL,
`column_6` time NOT NULL,
PRIMARY KEY (`column_1`),
UNIQUE KEY `index_1` (`column_2`,`column_3`),
UNIQUE KEY `index_2` (`column_2`,`column_4`),
KEY `index_3` (`column_3`),
KEY `index_4` (`column_4`),
KEY `index_5` (`column_2`),
KEY `index_6` (`column_5`,`column_2`),
CONSTRAINT `fk_1` FOREIGN KEY (`column_3`) REFERENCES `test2`(`id`),
CONSTRAINT `fk_2` FOREIGN KEY (`column_4`) REFERENCES `test2` (`id`),
CONSTRAINT `fl_3` FOREIGN KEY (`column_2`) REFERENCES `link` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14164023 DEFAULT CHARSET=utf8;
当我运行以下查询时,对于column_2的不同值,大约需要5-8秒。有人可以帮助更好地执行此操作吗?
SELECT count(*) FROM test WHERE test.column_2= 26 and
test.column_5 between '2015-06-01 00:00:00' AND
'2015-06-30 00:00:00' ;
注意:通过在mysql工作台上执行查询来捕获所提到的时间
答案 0 :(得分:1)
您的column_5
目前有column_2
,然后是BETWEEN
,因此MySQL首先尝试根据column_2
子句进行过滤。但是MySQL有限制,在范围模式下使用索引后,它不能使用索引的第二部分(此blog post中的更多信息)。
优化此类查询的正确方法是将 equation 列作为索引的第一部分,将 range 列作为第二列。然后,MySQL将选择26
值为column_5
的行,然后使用索引的第二部分根据KEY `ind_c2_c5` (`column_2`,`column_5`)
日期范围进一步过滤它们。
所以解决方案是有一个索引:
<?php
include_once('inc/class.simple_mail.php');
include_once('inc/gump.class.php');
include_once('mail-config.php');
// Check Data
$isValid = GUMP::is_valid($_POST, array(
'first-name' => 'required',
'last-name' => 'required',
'phone-number' => 'required',
'email-address' => 'required|valid_email',
'address' => 'required',
'city' => 'required',
'zip-code' => 'required',
));
if($isValid === true) {
// Submit Mail
$mail = new SimpleMail();
$mail->setTo(YOUR_EMAIL_ADDRESS, YOUR_COMPANY_NAME)
->setSubject('New car rental request')
->setFrom(htmlspecialchars($_POST['email-address']), htmlspecialchars($_POST['first-name'].' '.$_POST['last-name']))
->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage(createMessage($_POST))
->setWrap(100);
$mail->send();
// Submit Client Mail
$mailClient = new SimpleMail();
$mailClient->setTo(htmlspecialchars($_POST['email-address']), htmlspecialchars($_POST['first-name'].' '.$_POST['last-name']))
->setSubject('Youre car rental request at '.YOUR_COMPANY_NAME)
->setFrom(YOUR_EMAIL_ADDRESS, YOUR_COMPANY_NAME)
->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage(createClientMessage($_POST))
->setWrap(100);
$mailClient->send();
$result = array(
'result' => 'success',
'msg' => array('Success! Your contact request has been send.')
);
echo json_encode($result);
} else {
$result = array(
'result' => 'error',
'msg' => $isValid
);
echo json_encode($result);
}
function createMessage($formData)
{
$message = $_POST['first-name']. " Booked a ".$_POST['selected-car']."Contact :".$_POST['phone-number'];
//$message = $_POST['first-name']. "Booked a Tata Innova From Website"; //Corrected string
$user="demo";//don't change this thing
$password = "demopass";//don't change this thing
$from = "NANDHI"; // //don't change this thing - Sender ID
$to = "9566376463"; // Ph# txt message is sent to //Corrected double quote
$fields = array( ///URL Details that you are sending...
'user' => urlencode($user),
'password' => urlencode($password),
'phone' => urlencode($to),
'text' => urlencode($message),
'type' => 't',
'senderid' => urlencode($from),
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$url = 'http://login.redsms.in/API/SendMessage.ashx?'.$fields_string;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
curl_close($ch);
}
function createClientMessage($formData)
{
$body = "Hello ".htmlspecialchars($formData['first-name'])." ".htmlspecialchars($formData['last-name'])."<br><br>";
$body .= "We appreciate your interest in our offer. Your request has been successfully forwarded to us.<br>";
$body .= "We will deal with it immediately and contact you as soon as possible in contact with you.<br><br>";
$body .= "For further questions we are happy to help! <br><br>";
$body .= "Best regards<br>".YOUR_COMPANY_NAME;
return $body;
}
// $mail = new SimpleMail();
// $mail->setTo('mail@themeinjection.com', 'Your Email')
// ->setSubject('Test Message')
// ->setFrom('no-reply@domain.com', 'Domain.com')
// ->addMailHeader('Reply-To', 'no-reply@domain.com', 'Domain.com')
// ->addMailHeader('Cc', 'bill@example.com', 'Bill Gates')
// ->addMailHeader('Bcc', 'steve@example.com', 'Steve Jobs')
// ->addGenericHeader('X-Mailer', 'PHP/' . phpversion())
// ->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
// ->setMessage('<strong>This is a test message.</strong>')
// ->setWrap(100);
//$send = $mail->send();
//echo ($send) ? 'Email sent successfully' : 'Could not send email';
//echo json_encode(array('data' => 'test data'));
/* AJAX check */
// if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
// echo json_encode(array('data' => 'test data'));
// }
// else
// {
// echo 'no ajax';
// }
顺便说一下,给索引描述性名称更好,所以你一眼就知道它们的用途......