Skip to content
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

[4.x] Updated db command for clearing tables #991

Merged
merged 1 commit into from
Dec 7, 2020
Merged

[4.x] Updated db command for clearing tables #991

merged 1 commit into from
Dec 7, 2020

Conversation

theofanisv
Copy link
Contributor

@theofanisv theofanisv commented Dec 7, 2020

Hello, while clearing the db tables the delete statement being used is significantly slower than truncate.

The logs the delete keeps are useless since these tables contain only debugging info and not essential data.

While using telescope:clear it takes very much time for several million records whilst manually executing truncate takes a couple of seconds for about the same amount of records. Also it consumes fewer server resources. I think the statement should be changed from delete to truncate.

Truncate Statement
Truncate vs Delete

@driesvints driesvints changed the title Updated db command for clearing tables [4.x] Updated db command for clearing tables Dec 7, 2020
@taylorotwell taylorotwell merged commit 5c0d36a into laravel:4.x Dec 7, 2020
@Namoshek
Copy link
Contributor

Namoshek commented Dec 8, 2020

@Theograms @taylorotwell
This breaks the command when using an SQL Server database:

Msg 4712, Level 16, State 1, Line 1
Cannot truncate table 'dbo.telescope_entries' because it is being referenced by a FOREIGN KEY constraint.

I do not have much experience with the other supported DBMS, so I cannot tell if similar issues arise during their usage as well.

Edit:
According to this question on StackOverflow, the change also seems to be incompatible with MySql if not using any of the presented workarounds. PostgreSQL seems to handle TRUNCATE with foreign keys just fine, while SQLite doesn't even have a TRUNCATE command and uses an optimized DELETE FROM query to truncate a table if no WHERE conditions are given (the sqlite grammar converts the truncate() to a DELETE FROM though).

@driesvints
Copy link
Member

I'm reverting this

@louisgab
Copy link

@driesvints @taylorotwell May I suggest:

DB::connection($this->connection)->enableForeignKeyConstraints();
$this->table('telescope_entries')->truncate();
$this->table('telescope_monitoring')->truncate();
DB::connection($this->connection)->disableForeignKeyConstraints();

Maybe in a separate command telescope:truncate so it can be carefully used on supported DBMS ?

I would happily PR it

@driesvints
Copy link
Member

We're not reconsidering this atm sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants