字符串到值比较优化MySQL查询

[英]String to Value compare Optimizing MySQL Query


My problem is the following: I have two arrays $first and $second of the same length, containing strings. Every string is given a positive value in a table named Fullhandvalues:

我的问题如下:我有两个数组$ first和$ second相同的长度,包含字符串。在名为Fullhandvalues的表中,每个字符串都给出一个正值:

Field: board : string(7) PRIMARY KEY
Field: value : int (11)

I want to count how many times $first[$i] has a better value than $second[$i], how many times they have the same value, and how many times $first[$i] has a worse value than $second[$i].

我想计算$ first [$ i]有多少次价值比$ $ [$ i]更好,有多少次它们具有相同价值,以及$ first [$ i]有多少次价值比$ $更差第二[$ i]中。

What I have done now is getting all the values via

我现在所做的就是获得所有价值

$values[0]= DB::table('Fullhandvalues')->where_in("board",$first)->get(Array("value"));
$values[1]= DB::table('Fullhandvalues')->where_in("board",$second)->get(Array("value"));

and then comparing the values. But this seems to be very slow (approximately 6 seconds, for an array length of 5000 and 50000 entries in the table)

然后比较这些值。但这似乎非常慢(大约6秒,表中的数组长度为5000和50000个条目)

Thanks very much in advance

首先十分感谢

EDIT: How I loop through them:

编辑:我如何循环它们:

$win=0;$lose=0;$tie=0;
for($i=0;$i<count($values[0]);$i++)
    {
        if ($values[0][$i]>$values[1][$i])
            $win++;
        elseif ($values[0][$i]<$values[1][$i])
            $lose++;
        else $tie++;
    }

1 个解决方案

#1


1  

Your problem is where_in. You are basically building a query with the length of implode(',', $second) (plus change). This has to be first generated by Laravel (PHP) and then analysed by your DBMS.

你的问题是where_in。您基本上构建了一个长度为implode(',',$ second)的查询(加上更改)。这必须首先由Laravel(PHP)生成,然后由您的DBMS进行分析。

Also the generated query will use the IN(...) expression, which is known to be slow in MySQL.

此外,生成的查询将使用IN(...)表达式,已知该表达式在MySQL中很慢。

Without further information about the application and how board IDs are selected, here is an option you have:

如果没有关于应用程序的更多信息以及如何选择板ID,您可以选择以下选项:

  • Create a temp-table and fill it with your array data (this should be quite fast, but preferably this data should already be in the database)
  • 创建一个临时表并用数组数据填充它(这应该非常快,但最好这个数据应该已经存在于数据库中)

  • Don't forget to create an index on the temp table.
  • 不要忘记在临时表上创建索引。

  • Select with an inner join.
  • 选择内连接。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/02/10/d35b9276b2838f9363c2198b819a5212.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告