简体   繁体   English

PHP Laravel使用MYSQL获得数百万条记录

[英]PHP Laravel get millions record using MYSQL

I have been using laravel + mysql for my project but and it was working perfectly fine until now. 我一直在为我的项目使用laravel + mysql,但是到目前为止,它的运行情况还不错。 The records keep on increasing and now they have reached almost a million record. 记录不断增加,现在已经达到近一百万个记录。 Problem is when i try to fetch sms from my database using this query 问题是当我尝试使用此查询从数据库中获取短信时

 $smsHistory = SmsLog::where('created_at', '>=', $startDate)->where('created_at', '<=', $endDate)->whereNotNull('gateway_statuscode')->get();

It gives a 500 error without showing anything in error log. 它给出一个500错误,而在错误日志中不显示任何内容。 What i assume is that as i decreased the time period it gives me record so the problem is the bulk it can not handle. 我假设的是,随着我减少时间段,它给了我记录,因此问题是它无法处理的大量内容。 What could be the possible solution as i have to give it today. 我今天必须提供的可能的解决方案是什么。

I am not worried about the error log.. i want to get a count of those million record but i want to apply some algorithm on it before doing it 我不担心错误日志..我想获得那百万条记录的计数,但是我想在执行之前对其应用一些算法

This is the check which i have to perform afterwards to see how many sms are in sms log 这是我之后必须执行的检查,以查看短信日志中有多少短信

 foreach ($smsHistory as $sms) {
        $sms_content = SmsService::_getSmsContent($sms);
        if ($sms_content->business_id && array_key_exists($sms_content->business_id, $smsCredits )) {
            if (floor(strlen($sms_content->content) / 160) == 0) {
                $smsCredits[$sms_content->business_id]['count'] += 1;
            }

            if (floor(strlen($sms_content->content) / 160) == 1 && floor(strlen($sms_content->content) / 306) == 0) {
                $smsCredits[$sms_content->business_id]['count'] += 2;
            }

            if (floor(strlen($sms_content->content) / 306) == 1 && floor(strlen($sms_content->content) / 459) == 0) {
                $smsCredits[$sms_content->business_id]['count'] += 3;
            }

            if (floor(strlen($sms_content->content) / 459) == 1 && floor(strlen($sms_content->content) / 621) == 0) {
                $smsCredits[$sms_content->business_id]['count'] += 4;
            }

            if (floor(strlen($sms_content->content) / 621) == 1 && floor(strlen($sms_content->content) / 774) == 0) {
                $smsCredits[$sms_content->business_id]['count'] += 5;
            }

            if (floor(strlen($sms_content->content) / 774) == 1 && floor(strlen($sms_content->content) / 927) == 0) {
                $smsCredits[$sms_content->business_id]['count'] += 6;
            }
        }

this is the database field 这是数据库字段 在此处输入图片说明

content is the sms that i have to get and count 内容是我必须获取并计数的短信

Regarding the 500 error: If you are getting a 500 error, there should hopefully be some clue in the actual server error log (your laravel application error log may not have caught it depending on the error handlers, etc and what the cause was). 关于500错误:如果遇到500错误,则希望在实际的服务器错误日志中有一些线索(您的laravel应用程序错误日志可能未捕获到该错误,具体取决于错误处理程序等,以及原因是什么)。 php_info() should show you the location of the physical error log with the error_log setting: php_info()应该使用error_log设置显示物理错误日志的位置:

<?php phpinfo(); ?>

If I had to guess, possibly something memory related that is causing it to choke. 如果我不得不猜测,可能是与内存相关的某种原因导致其阻塞。 But that is just a guess. 但这只是一个猜测。

As a side question, why are you trying to retrieve so many at once? 附带的问题是,为什么要尝试一次检索这么多? Can you split them up somehow? 你能以某种方式拆分它们吗?

Edit based on updated question: You may need to use some raw-expressions here to get what you really want: https://www.laravel.com/docs/4.2/queries#raw-expressions 根据更新的问题进行编辑:您可能需要在此处使用一些原始表达式来获得真正想要的内容: https : //www.laravel.com/docs/4.2/queries#raw-expressions

MySQL for example provides the ability to get a column length: FLOOR(CHAR_LENGTH(content) / 160) as len1, FLOOR(CHAR_LENGTH(content) / 306) as len2, FLOOR(CHAR_LENGTH(content) / 459) as len3 例如,MySQL提供了获取列长度的功能:FLOOR(CHAR_LENGTH(content)/ 160)为len1,FLOOR(CHAR_LENGTH(content)/ 306)为len2,FLOOR(CHAR_LENGTH(content)/ 459)为len3

So probably with some magic we could take your query and let the database system do it all for you. 因此,也许可以借助一些魔术来接受您的查询,然后让数据库系统为您完成这一切。 And there are probably more efficient ways to do it, if I knew more about the significance of those numbers, but I am trying to help you at least get on one possible path. 如果我更多地了解这些数字的重要性,可能会有更有效的方法,但是我正在努力帮助您至少走上一条可行的道路。

您应该为mysql设置索引,或实现像弹性搜索这样的搜索引擎

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM