summaryrefslogtreecommitdiff
path: root/doc/development/database_helpers.md
blob: 21e4e725de6f26742bfb9892ddd75143eb562253 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Database helpers

There are a number of useful helper modules defined in `/lib/gitlab/database/`.

## Subquery

In some cases it is not possible to perform an operation on a query.
For example:

```ruby
Geo::EventLog.where('id < 100').limit(10).delete_all
```

Will give this error:

> ActiveRecord::ActiveRecordError: delete_all doesn't support limit

One solution would be to wrap it in another `where`:

```ruby
Geo::EventLog.where(id: Geo::EventLog.where('id < 100').limit(10)).delete_all
```

This works with PostgreSQL, but with MySQL it gives this error:

> ActiveRecord::StatementInvalid: Mysql2::Error: This version of MySQL
> doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Also, that query doesn't have very good performance. Using a
`INNER JOIN` with itself is better.

So instead of this query:

```sql
SELECT geo_event_log.*
FROM geo_event_log
WHERE geo_event_log.id IN
    (SELECT geo_event_log.id
     FROM geo_event_log
     WHERE (id < 100)
     LIMIT 10)
```

It's better to write:

```sql
SELECT geo_event_log.*
FROM geo_event_log
INNER JOIN
  (SELECT geo_event_log.*
   FROM geo_event_log
   WHERE (id < 100)
   LIMIT 10) t2 ON geo_event_log.id = t2.id
```

And this is where `Gitlab::Database::Subquery.self_join` can help
you. So you can rewrite the above statement as:

```ruby
Gitlab::Database::Subquery.self_join(Geo::EventLog.where('id < 100').limit(10)).delete_all
```

And this also works with MySQL, so you don't need to worry about that.