如何在Laravel框架中以最少的时间从数据库中取出一个普通数组中的所有单词?
How to take all the words in one normal array from the database in the Laravel framework with a minimum waste of time?
我想从数据库中取出所有的单词到默认数组。我在数据库中有 50,000 多个单词,这个数字很可能高达一百万。因此,我想这个操作并没有花多少时间。我尝试了这样的方法,其中没有一个词被放入通常的数组中。也就是传给关联数组的词:
$words = DB::table('words')->pluck('word');
dump($words);
结果:
Collection {#197 ▼
#items: array:12 [▼
0 => "тоҷик"
1 => "ӯзбек"
2 => "қирғиз"
3 => "эрон"
4 => "япон"
5 => "англис"
6 => "тоҷик"
7 => "ӯзбек"
8 => "қирғиз"
9 => "эрон"
10 => "япон"
11 => "англис"
]
}
第二种方法:
$words = DB::select("SELECT `word` FROM `words`");
dump($words);
结果:
array:12 [▼
0 => {#210 ▼
+"word": "тоҷик"
}
1 => {#207 ▼
+"word": "ӯзбек"
}
2 => {#209 ▼
+"word": "қирғиз"
}
3 => {#206 ▼
+"word": "эрон"
}
4 => {#208 ▼
+"word": "япон"
}
5 => {#205 ▼
+"word": "англис"
}
6 => {#204 ▼
+"word": "тоҷик"
}
7 => {#203 ▼
+"word": "ӯзбек"
}
8 => {#202 ▼
+"word": "қирғиз"
}
9 => {#200 ▼
+"word": "эрон"
}
10 => {#213 ▼
+"word": "япон"
}
11 => {#214 ▼
+"word": "англис"
}
]
我想把这种形式的所有单词都放在通常的数组中:
array:12 [▼
0 => "тоҷик"
1 => "ӯзбек"
2 => "қирғиз"
3 => "эрон"
4 => "япон"
5 => "англис"
6 => "тоҷик"
7 => "ӯзбек"
8 => "қирғиз"
9 => "эрон"
10 => "япон"
11 => "англис"
]
我需要这个来区分两个数组。输入数组是一个乐观的数组,所以我需要从低音中提取所有的词来添加到丰富的数组中。一般来说,Laravel框架本身有没有类似array_diff()
的功能?
$diff = array_diff($input_array, $words_from_db);
并将差异添加到数据库中:
foreach ($diff as $value) {
DB::table('words')->insert(['word'=>$value]);
}
我向数据库中添加大量单词(数据)的方法正确吗?如果可能的话,我需要优化吗?
总的来说我从Laravel:
找到了这个函数
public function diff($items)
{
return new static(array_diff($this->items, $this->getArrayableItems($items)));
}
但是我如何使用它来获取 diff 2 数组?输入数组将是默认数组和来自 db
的第二个数组字
预先感谢大家的回答,对于问题中的语法错误,我深表歉意。
您可以完全在 MySQL 内完成将唯一词插入数据库,这总是比将所有词拉入内存、区分它们并发送唯一值更快。
为此,您需要在单词列中添加唯一索引。 (参考:https://dev.mysql.com/doc/refman/5.7/en/create-index.html)
CREATE UNIQUE INDEX `idx_word` ON `db`.`words` (word);
然后在插入时需要添加 ON DUPLICATE KEY
(Laravel 查询生成器不支持重复键 - 可以使用原始插入)参考:https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html, https://laravel.com/docs/5.5/queries#raw-expressions
INSERT INTO word VALUES ('foo'),('bar') ON DUPLICATE KEY word = VALUES(word);
我会使用重复键而不是插入忽略。我认为插入忽略可能会隐藏与重复键无关的其他潜在问题。
你可以在 Laravel 5.5 中得到 diff 两个数组,所以:
$words = DB::table('words')->pluck('word')->toArray(); // Collection converted to simple array
并且您可以使用 php 中的默认函数来获取差异数组:
$result = array_diff($input_default_array, $words);
我想从数据库中取出所有的单词到默认数组。我在数据库中有 50,000 多个单词,这个数字很可能高达一百万。因此,我想这个操作并没有花多少时间。我尝试了这样的方法,其中没有一个词被放入通常的数组中。也就是传给关联数组的词:
$words = DB::table('words')->pluck('word');
dump($words);
结果:
Collection {#197 ▼
#items: array:12 [▼
0 => "тоҷик"
1 => "ӯзбек"
2 => "қирғиз"
3 => "эрон"
4 => "япон"
5 => "англис"
6 => "тоҷик"
7 => "ӯзбек"
8 => "қирғиз"
9 => "эрон"
10 => "япон"
11 => "англис"
]
}
第二种方法:
$words = DB::select("SELECT `word` FROM `words`");
dump($words);
结果:
array:12 [▼
0 => {#210 ▼
+"word": "тоҷик"
}
1 => {#207 ▼
+"word": "ӯзбек"
}
2 => {#209 ▼
+"word": "қирғиз"
}
3 => {#206 ▼
+"word": "эрон"
}
4 => {#208 ▼
+"word": "япон"
}
5 => {#205 ▼
+"word": "англис"
}
6 => {#204 ▼
+"word": "тоҷик"
}
7 => {#203 ▼
+"word": "ӯзбек"
}
8 => {#202 ▼
+"word": "қирғиз"
}
9 => {#200 ▼
+"word": "эрон"
}
10 => {#213 ▼
+"word": "япон"
}
11 => {#214 ▼
+"word": "англис"
}
]
我想把这种形式的所有单词都放在通常的数组中:
array:12 [▼
0 => "тоҷик"
1 => "ӯзбек"
2 => "қирғиз"
3 => "эрон"
4 => "япон"
5 => "англис"
6 => "тоҷик"
7 => "ӯзбек"
8 => "қирғиз"
9 => "эрон"
10 => "япон"
11 => "англис"
]
我需要这个来区分两个数组。输入数组是一个乐观的数组,所以我需要从低音中提取所有的词来添加到丰富的数组中。一般来说,Laravel框架本身有没有类似array_diff()
的功能?
$diff = array_diff($input_array, $words_from_db);
并将差异添加到数据库中:
foreach ($diff as $value) {
DB::table('words')->insert(['word'=>$value]);
}
我向数据库中添加大量单词(数据)的方法正确吗?如果可能的话,我需要优化吗?
总的来说我从Laravel:
找到了这个函数public function diff($items)
{
return new static(array_diff($this->items, $this->getArrayableItems($items)));
}
但是我如何使用它来获取 diff 2 数组?输入数组将是默认数组和来自 db
的第二个数组字预先感谢大家的回答,对于问题中的语法错误,我深表歉意。
您可以完全在 MySQL 内完成将唯一词插入数据库,这总是比将所有词拉入内存、区分它们并发送唯一值更快。
为此,您需要在单词列中添加唯一索引。 (参考:https://dev.mysql.com/doc/refman/5.7/en/create-index.html)
CREATE UNIQUE INDEX `idx_word` ON `db`.`words` (word);
然后在插入时需要添加 ON DUPLICATE KEY
(Laravel 查询生成器不支持重复键 - 可以使用原始插入)参考:https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html, https://laravel.com/docs/5.5/queries#raw-expressions
INSERT INTO word VALUES ('foo'),('bar') ON DUPLICATE KEY word = VALUES(word);
我会使用重复键而不是插入忽略。我认为插入忽略可能会隐藏与重复键无关的其他潜在问题。
你可以在 Laravel 5.5 中得到 diff 两个数组,所以:
$words = DB::table('words')->pluck('word')->toArray(); // Collection converted to simple array
并且您可以使用 php 中的默认函数来获取差异数组:
$result = array_diff($input_default_array, $words);