Say I have a given table with the following data:
| id | pid | email | entries |
|----|-----|-----------------|---------|
| 1 | 1 | [email protected] | 3 |
| 2 | 1 | [email protected] | 1 |
| 3 | 1 | [email protected] | 2 |
id
is the primary key.
pid,email
has a unique index.
entries
indicates the number of entries the person has into a prize draw.
How can I query the above data to get the following result:
| id | pid | email | entries |
|----|-----|-----------------|---------|
| 1 | 1 | [email protected] | 3 |
| 1 | 1 | [email protected] | 3 |
| 1 | 1 | [email protected] | 3 |
| 2 | 1 | [email protected] | 1 |
| 3 | 1 | [email protected] | 2 |
| 3 | 1 | [email protected] | 2 |
where each row is returned n times based on the entries column a row has?
The reason is to hand the result to a randomized name drawer where the more entries you have the more chances you have of winning.
Currently, we have the setup where each entry creates a new row in the table but this starts having performance issues when certain entrants make a thousand entries in one go. This generates thousands of inserts at a time and when we have a popular prize we are running into performance issues where inserts start taking too long as there are so many.
If we could restructure each how we store the data we can then just have 1 upsert query when, for example, someone gets 1000 entries in one go rather than generating 1000 individual inserts.