简体   繁体   English

限制所有MySQL查询的结果

[英]Limit results of all MySQL queries

I wrote a PHP/MySQLi frontend, in which the user can enter SQL queries, and the server then returns the results in a table (or prints OK on INSERT s and UPDATE s) 我编写了一个PHP / MySQLi前端,用户可以在其中输入SQL查询,然后服务器将结果返回到表中(或在INSERTUPDATE上打印OK)。

As printing the results can take a very long time (eg SELECT * FROM movies ) in a IMDb extract with about 1.6M movies, 1.9M actors and 3.2M keywords, I limited the output to 50 rows by cancelling the printing for-loop after 50 iterations. 由于打印结果可能需要很长的时间(例如SELECT * FROM movies ),而包含大约160万部电影,190万个actor和320万个关键字的IMDb提取物中,我通过取消打印循环后将输出限制为50行50次迭代。

However, the queries themselves also take quite some time, so I hoped that it might be possible to set a global maximum row return value, nevertheless whether the LIMIT keyword is used or not. 但是,查询本身也要花费一些时间,因此我希望可以设置全局最大行返回值,无论是否使用LIMIT关键字。 I only intended to use the server for my own practice, but as some people in my class are struggling with the frontend provided by the teacher (Windows EXE, but half of the class uses Mac/Linux), I decided to make it accessible to them, too. 我只是打算将服务器用于自己的实践,但是由于班上的某些人在为老师提供的前端而苦苦挣扎(Windows EXE,但班上的一半使用Mac / Linux),因此我决定使其可以访问他们也是。 But I want to keep my Debian VM from crashing because of - well, basically it would be a DDoS. 但是我想防止我的Debian VM崩溃,因为-基本上,这将是DDoS。

For clarification (examples with a global limit of 50): 为了澄清(示例,全局限制为50):

SELECT * FROM movies;
> First 50 rows
SELECT * FROM movies LIMIT 10;
> First 10 rows
SELECT * FROM movies LIMIT 50,100;
> 50 rows (from 50 to 99)

Is there any possibility to limit the number of returned values using either PHP/MySQLi or the MySQL server itself? 是否有可能使用PHP / MySQLi或MySQL服务器本身来限制返回值的数量? Or would I have to append/replace LIMIT to/in the queries? 还是我必须在查询中附加/替换LIMIT

You can use there queries and add "LIMIT 50" to it. 您可以在此处使用查询,并在其中添加“ LIMIT 50”。

And if they added LIMIT by them self just filter it out with regex and still add your LIMIT. 如果他们自己添加了LIMIT,则只需使用正则表达式将其过滤掉,然后仍然添加LIMIT。

I believe you have to build yourself a paginator anyway, avoiding to use LIMIT statement is not really possible i believe. 我相信您无论如何都必须建立自己的分页器,我相信避免使用LIMIT语句是不可能的。

Here is what I would suggest for a Paginator: 这是我对分页器的建议:

if($_REQUEST['page'] == ""){
 $page = 1;
}else{
 $page = $_REQUEST['page']; // perhaps double check if numeric
}

$perpage = 50;
$start = ($page - 1) * $perpage;

$limit_string = " LIMIT ". $start . "," . $perpage ;

$query = "SELECT * FROM movies";
$query .= $limit_string;

Hope that helps 希望能有所帮助

You can create a function. 您可以创建一个函数。 https://dev.mysql.com/doc/refman/5.0/en/create-function.html https://dev.mysql.com/doc/refman/5.0/en/create-function.html

Let us know if this helps. 让我们知道是否有帮助。

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

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