Slug generation performance improvement#118
Conversation
The `EXISTS` sql statement will return true at the first existing value where the `COUNT` will uselessly continue to loop through the whole table which is inaccurate for this use case. The syntax is more readable too IMO.
Will conflict with wintercms#118
|
I've given this a test by running the following in the Sluggable tests: public function testSlugGenerationLargeDataset()
{
// Create 2500 records
for ($i = 0; $i < 2500; ++$i) {
TestModelSluggable::create([
'name' => 'test row ' . $i,
]);
}
$testModel1 = TestModelSluggable::create(['name' => 'test']);
// Create another 2500 records
for ($i = 2500; $i < 5000; ++$i) {
TestModelSluggable::create([
'name' => 'test row ' . $i,
]);
}
DB::listen(function ($query) {
print_r([
'query' => $query->sql,
'bindings' => $query->bindings,
'time' => $query->time,
]);
});
$testModel2 = TestModelSluggable::create(['name' => 'test']);
}I found a very, very, very small performance benefit for using The logic is sound, so I'm not against merging it - it makes sense that we only care if a record exists with a given slug, we don't need to know how many records have the slug, but I just wanted to make it clear this is a micro-optimisation. |
|
While the gains are small, the change does take it from an issue that gets slower with scale and turns it into a consistent performance operation instead which is always better IMO. |
The
EXISTSsql statement will return true at the first existing value where theCOUNTwill uselessly continue to loop through the whole table which is inaccurate for this use case.The syntax is more readable too IMO.