Django ORM's contains vs SQL's LIKE

情到浓时终转凉″ 提交于 2019-12-11 15:27:52

问题


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

来源:https://stackoverflow.com/questions/57004942/django-orms-contains-vs-sqls-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!