Try this:
- Use
LOAD DATA INFILE .. IGNORE ..
.
- Add temporary trigger to this table like
CREATE TRIGGER prevent_excess_lines_insertion
BEFORE INSERT
ON users
FOR EACH ROW
BEGIN
IF 50000 < (SELECT COUNT(*) FROM USERS) THEN
SET NEW.id = 1;
END IF;
END
When the line is loaded then the amount of rows in the table (except the line to be inserted) is counted and compared with pre-defined rows amount (50000).
If current rows amount is less then the row is inserted.
If predefined amount is reached then some predefined value (1) is assigned to primary key column. This causes unique constraint violation, which is ignored due to IGNORE
modifier.
In this case the whole file will be nevertheless loaded (but only needed rows amount will be inserted).
If you want to break the process then remove IGNORE
modifier and replace SET
statement with SIGNAL
which sets generic SQL error, and loading process will be terminated.
Do not forget to remove the trigger immediately after performing the import.
Note that COUNT(*)
in InnoDB can be pretty slow on large tables. Doing it before each insert might make the load take a while. – Barmar
This is true :(
You may use user-defined variable instead of querying the amount of rows in a table. The trigger will be
CREATE TRIGGER prevent_excess_lines_insertion
BEFORE INSERT
ON users
FOR EACH ROW
BEGIN
IF (@needed_count := @needed_count - 1) < 0 THEN
SET NEW.id = 1;
END IF;
END
Before insertion you must set this variable to the amount of rows to be loaded, for example, SET @needed_count := 50000;
. Variable must be set in the same connection strictly !!! And variable's name must not interfere with another variables names if they're used.