-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use INSERT...ON DUPLICATE KEY UPDATE optimization #19
Comments
I'd definitely love to improve it and have a better query for each database. I'm not sure yet what would be the best way. I was thinking about a macro on the model which registers the correct SQL query depending on your database |
@julienbourdeau I like this idea. I stumbled across a method available on the DB facade that might help with this too? DB::getDriverName() // e.g. 'sqlsrv' |
Actually, it should be possible to support multiple databases using this package: From the README
|
i'll have a look 👍 |
As of Laravel 8.10 there is built-in support for upsert. laravel/framework#34698 |
Currently, the code uses
RouteUsage::updateOrCreate()
which first performs aSELECT
query and then either anINSERT
orUPDATE
query. It is doing 2 queries for every request. If using MySQL then we can useINSERT...ON DUPLICATE KEY UPDATE
query which will only need to do 1 query and since theidentifier
column has a UNIQUE index, it will automatically do an update if the identifier already exists.I don't think Laravel supports those types of queries but you can use a library like:
https://github.com/yadakhov/insert-on-duplicate-key
The code might look something like this:
I assume that doing 1 query instead of 2 would be faster but some testing would need to be done to see if it is actually faster.
UPDATE: I just noticed in #4 that you used to use this type of query and then removed it to support other databases. If you make it a config option then people using MySQL can use the optimization and other databases will continue to use
updateOrCreate()
The text was updated successfully, but these errors were encountered: