views:

25

answers:

1

The MYSQL rand() function is notoriously slow in select statements, is this true for insert statements as well? I would like to insert a new row with random number in the following way:

insert into new_table (field1, field2, randomField) values ('Hello', 'Ola', rand());

Will the rand() function become slow as the table gets larger and larger?

+2  A: 

Will the rand() function become slow as the table gets larger and larger?

No. RAND() getting slower on big tables is usually when it is used in conjunction with ORDER BY or WHERE. In your example, you are making one call to RAND() which will not be a performance problem.

Pekka
Indeed, and it's only slow in `ORDER BY` (and potentially `WHERE`) because it defeats MySQL's abaility to use an index to pick or sort the data. The `RAND()` call itself is cheap.
bobince