我一直在试图找出一种有效的方法来将一个月的点击数据分解为图表的单独日期,但到目前为止,我所汇总的大部分查询都需要20-30秒,因为我在没有子表\子查询的情况下,我无法想办法。到目前为止,我想出的最好的是:
`<?php
function load_district()
{
$connect = mysqli_connect("localhost","root","") or
die(mysqli_error($connect));
mysqli_select_db($connect,"Ajmal") or die(mysqli_error($connect));
$output='';
$sql="SELECT DISTINCT district FROM address";
$result=mysqli_query($connect,$sql);
while($row = mysqli_fetch_array($result)){
$output .= '<option
value .= "'.$row["district"].'">'.$row["district"].'</option>';
}
return $output;
}
?>
<html>
<head>
<title>Ajmal Hossain</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
</head>
<body>
<p>Select District
<select name="district" id="district">
<option value="">Select District</option>
<?php echo load_district(); ?>
</select>
</p>
<p>Select Place
<select name="place" id="place">
<option value="">Select place</option>
</select>
</p>
</body>
</html>
<script>
$(document).ready(function(){
$('#district').change(function(){
var district_id = $(this).val();
$.ajax({
url:"fetch_state.php",
method:"POST",
data:{districtID:district_id},
dataType:"text",
success:function(data){
$('#place').html(data);
}
});
});
});
</script>`
说明:
`<?php
$connect = mysqli_connect("localhost","root","") or
die(mysqli_error($connect));
mysqli_select_db($connect,"ajmal") or die(mysqli_error($connect));
$output = '';
$sql="SELECT place FROM address WHERE district =
'".$_POST["districtID"]."'";
$result = mysqli_query($connect,$sql);
$output = '<option value="">Select Place</option>';
while($row=mysqli_fetch_array($result)){
$output = '<option
value .= "'.$row["place"].'">'.$row["place"].'</option>';
}
echo $output;
?>`
或其变体,但考虑到潜在的记录数量(每天可能获得10万次点击),它仍然非常低效。此外,实际代码使用预处理语句,因此无需指出,为了清楚起见,我将值放在这里。
编辑:我发现使用以下内容的速度更快,但我担心当系统上有更多用户组时会遇到问题。
SELECT
SUM(CASE WHEN ( TIME BETWEEN '2018/04/09' AND '2018/04/10') THEN 1 ELSE 0 END) 9th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/08' AND '2018/04/09') THEN 1 ELSE 0 END) 8th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/07' AND '2018/04/08') THEN 1 ELSE 0 END) 7th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/06' AND '2018/04/07') THEN 1 ELSE 0 END) 6th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/05' AND '2018/04/06') THEN 1 ELSE 0 END) 5th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/04' AND '2018/04/05') THEN 1 ELSE 0 END) 6th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/03' AND '2018/04/04') THEN 1 ELSE 0 END) 4th,
SUM(CASE WHEN ( TIME BETWEEN '2018/04/02' AND '2018/04/03') THEN 1 ELSE 0 END) 3rd
FROM
(
SELECT TIME, BIN_IP FROM CLICKS_IN WHERE USER_GROUP = 4 AND TIME BETWEEN '2018/04/02' AND '2018/04/10'
)a;
答案 0 :(得分:0)
SELECT SUM(TIME >= '2018-04-09' AND TIME < '2018-04-10') as 9th,
SUM(TIME >= '2018-04-08' AND TIME < '2018-04-09') as 8th
FROM CLICKS_IN
WHERE USER_GROUP = 4
AND TIME >= '2018-04-02'
AND TIME < '2018-04-11'
并确保您在time
和user_group
列上有索引。然后它应该在几毫秒内运行。
答案 1 :(得分:0)
您可以在每天作为一行返回的地方执行此操作。在调用PHP代码中,可以更有效地完成从行到列的透视。
SELECT
DAYOFMONTH(TIME) as `day`,
COUNT(*) as `numclicks`
FROM `CLICKS_IN`
WHERE USER_GROUP = 4 AND TIME BETWEEN '2018/04/02' AND '2018/04/10'
GROUP BY DAYOFMONTH(TIME)
ORDER BY DAYOFMONTH(TIME)