3

im using django-tables2 in order to show values from a database query. And everythings works fine. Im now using Django-dabug-toolbar and was looking through my pages with it. More out of curiosity than performance needs. When a lokked at the page with the table i saw that the debug toolbar registerd over 300 queries for a table with a little over 300 entries. I dont think flooding the DB with so many queries is a good idea even if there is no performance impact (at least not now). All the data should be coming from only one query.

Why is this happening and how can i reduce the number of queries?

user2602386
  • 120
  • 6

2 Answers2

2

Im posting this as a future reference for myself and other who might have the same problem.

After searching for a bit I found out that django-tables2 was sending a single query for each row. The query was something like SELECT * FROM "table" LIMIT 1 OFFSET 1 with increasing offset.

I reduced the number of sql calls by calling query = list(query) before i create the table and pass the query. By evaluating the query in the python view code the table now seems to work with the evaulated data instead and there is only one database call instead of hundreds.

user2602386
  • 120
  • 6
1

This was a bug and has been fixed in https://github.com/bradleyayers/django-tables2/issues/427

Inti
  • 3,443
  • 1
  • 29
  • 34