Django ORM's contains vs SQL's LIKE

2019-08-19 04:08发布

I'm working on Django 2.1.5 and MySQL 14.14.

I'm trying to query using a wildcard search as below for single character replacement.

  • 98765?321 - returns all the numbers in the database column with a single replacement for ? - at most 10 numbers will be returned.
  • 98?65?321 - returns all the numbers with single replacement for both ?s - at most 100 numbers will be returned.
  • 98?65?32? - returns all the numbers with single replacement for first ? and all the numbers ending with that for second ?- any number of numbers could be returned.
  • 98?65?3?1 - returns all the numbers with single replacement for all three ?s - at most 1000 numbers will be returned.
  • 987? should return all the numbers starting with 987 - could return any number of numbers.

I was able to do this with Model.objects.filter(column_regex=regex).

But as the DB contains 50 millions of rows, this is impacting performance.

How can I do the same thing with column__contains or column__icontains etc.

Sample DB column values:

9875156245
7657676372
3765277678
7765725757

0条回答
登录 后发表回答