I’ve encountered a problem with my Django website. Given that my database contains more than 20 million records, the operation of counting the total number of data (count) becomes extremely slow. I am using the following queries:
company_count_query = f"SELECT COUNT(DISTINCT cs.OGRN) AS total_companies {company_from_clause} WHERE {company_match_condition} {email_condition} {capital_condition} {date_condition} {company_okved_condition}"
ip_count_query = f"SELECT COUNT(DISTINCT ip.OGRNIP) AS total_ips {ip_from_clause} WHERE {ip_match_condition} {imail_condition} {ip_okved_condition}"
How can I optimize these queries or use other methods to speed up the calculation of the total number of records?
Thank you for your help!
I tried to optimize the query structure by adding indexes to the columns on which filtering is performed, hoping to improve the performance of the counting operation. I expected that this would reduce the query execution time and speed up the data processing. However, in practice, the query execution time remained high and the counting operation still takes too long.
Mlofor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.