简体   繁体   English

使用大型数据库时,Ajax请求需要很长时间才能完成

[英]Ajax request takes too long to complete when working with large database

I am working on a website written in Yii framework (version 1.1.14) that allows uploading and displaying news. 我正在使用Yii框架 (版本1.1.14)编写的网站,该网站允许上传和显示新闻。 The admin of the site can select three news to promote to homepage and specify the order in which they are displayed. 网站的管理员可以选择三个新闻来推广到主页并指定它们的显示顺序。 I am using Mysql database. 我正在使用Mysql数据库。 The news table has two fields: isChecked (0 or 1) and homepagePos (integer) in addition to the other fields. 新闻表有两个字段: isChecked (0或1)和homepagePos (整数)以及其他字段。 The isChecked field determines whether the news is selected for displaying in homepage and the homepagePos field determines the order in which the news are displayed. isChecked字段确定是否选择了新闻以在主页中显示,并且homepagePos字段确定新闻的显示顺序。 I have used jquery's sortable plugin to sort the news. 我使用了jquery的可排序插件来对新闻进行排序。 When the user selects which news to display and clicks save button, the news ids are sent to php via ajax. 当用户选择要显示的新闻并单击“保存”按钮时,新闻ID将通过ajax发送到php。

The javascript portion to send the values to news controller is as follows: 将值发送到新闻控制器的javascript部分如下:

$(document).on('click', '#saveToHomepage', function() 
{
    var url = ajaxRequestSendUrl;   //ajaxRequestSendUrl contains url to news controller's promote to homepage method.
    $.ajax({
        method: "GET",
        url: url,
        data: {
            contentIds: contentIds, //contentIds contains an array of news Ids in certain order
            },
        success: function() {
        // Show success message
        },
        error: function() {
            alert('Some error occured. Please reload the page and try again.');
        }
    });
});

Here's the promote to homepage method in news controller: 这是新闻控制器中的主页推广方法:

public function actionHomepage()
{
    $allNews = News::model()->findAll();
    $value = $_GET['contentIds'];
    foreach ($allNews as $news) {
        if($news->id == $value[0] ||$news->id == $value[1] ||$news->id == $value[2])
        {
            $news->isChecked = 1;
            $news->homepagePos = array_search($news->id, $value); //Assign index of the array as the position
            $news->save();
        }
        else
        {
            $news->isChecked = 0;
            $news->homepagePos = -1;
            $news->save();
        }
    }
}

My problem is that the news table I have has over 2k data. 我的问题是我的新闻表有超过2k的数据。 So the ajax call takes really long time (over a minute) to complete. 所以ajax调用需要很长时间(超过一分钟)才能完成。 Is there any way I can optimize the code or is there other way I can approach this to reduce the time taken to complete this operation? 有什么方法可以优化代码,还是有其他方法可以解决这个问题,以减少完成此操作所需的时间? Thanks in advance 提前致谢

i am not sure, but why don't you get the filtered records from the database itself by sending the id's of selected news. 我不确定,但为什么不通过发送所选新闻的ID来从数据库本身获取过滤记录。 I don't know your backend language. 我不知道你的后端语言。 Seems, you are getting all the records and applying filtering. 好像,您正在获取所有记录并应用过滤。 which consumes time. 这耗费时间。 Instead get the filtered news from the database. 而是从数据库中获取过滤的新闻。

Hope this helps! 希望这可以帮助!

Its taking a long time because you're fetching all the records and then updating those three id . 它需要很长时间,因为你正在获取所有记录,然后更新这三个id You could try this: 你可以试试这个:

$criteria = new CDbCriteria;
$criteria->addInCondition( "id" , $value ) ; // $value = array ( 1, 2, 3 );
News::model()->updateAll(array('isChecked'=>'1','homepagePos'=>'val2'), $criteria);

You could do an update before hand to reset all the homepagePos and then update only the ids you need. 您可以事先进行更新以重置所有homepagePos ,然后仅更新您需要的ID。

Three queries: One first to set the whole table to not checked status, and the rest to set the checked status only in the row each selected id 三个查询:首先将整个表设置为未检查状态,其余为仅在每个选中的行中设置检查状态

public function actionHomepage()
{
    $values = $_GET['contentIds'];
    $sql = "UPDATE news SET idChecked=0,homepagePos = -1";
    Yii::app()->db
        ->createCommand($sql)
        ->execute();
    for($ii = 0; $ii < 3; $ii++) {
        $sql = "UPDATE news SET idChecked = 1,homepagePos = ':homepagePos' WHERE id=:id";
        Yii::app()->db
            ->createCommand($sql)
            ->bindValues(array(':homepagePos' => array_search($ii, $values), ':id' => $values[$ii]))
            ->execute();
    }
}

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

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