# Profiling django views for SQL queries

DevFeed: [Profiling django views for SQL queries](<https://devfeed.tech/articles/profiling-django-views-for-sql-queries-20002.md>)

Original publisher: [Read original article](<http://engineering.hackerearth.com/2016/02/01/profiling-django-views/>)

Published: 2016-02-01T00:00:00Z

Content type: article

Language: en

Sources: [HackerEarth](<https://devfeed.tech/sources/hackerearth.md>)

Topics: [Django](<https://devfeed.tech/topics/django.md>), [SQL](<https://devfeed.tech/topics/sql.md>), [Python](<https://devfeed.tech/topics/python.md>), [Code](<https://devfeed.tech/topics/code.md>), [Hackathon](<https://devfeed.tech/topics/hackathon.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [django](<https://devfeed.tech/tags/django.md>), [hackathon](<https://devfeed.tech/tags/hackathon.md>), [profiling](<https://devfeed.tech/tags/profiling.md>), [python](<https://devfeed.tech/tags/python.md>), [sql](<https://devfeed.tech/tags/sql.md>)

## AI overview

The article describes a profiler for Django views that identifies the exact code expressions causing SQL queries. It uses Python AST manipulation to instrument function calls and attribute access, while tracking queries generated during execution.

## Source excerpt

We at HackerEarth regularly conduct 24-hours internal hackathons usually once a month to boost ourselves to get familiar with new technologies and to come up with great ideas and hacks to increase our productivity. A hackathon project can be anything from creating a new product to creating some tools which helps our own devlopment. In the hakathon in Dec 2015, I came up with the idea to create a profiler which could tell which code inside django views causes SQL queries so that we can optimize them easily. Initial thoughts There are already many good django packages to profile views for SQL queries. One of them is django-toolbar which we already use. Django-toolbar is great but it shows all raw SQL queries which are happening inside a view and you have to analyze each query, see the whole traceback and figure out which line of code inside the view is triggering the query. This way, you can only figure out the line number of the code in a file, not the exact function or expression or attribute access which is causing the query. What I wanted was that a profiler should tell about the exact expressions which trigger the SQL queries. There were some solutions which came up in my mind to get it done like using tracebacks or by manipulating AST of the python function. Praveen had just told me about the python's ast module which can parse and modify code during runtime and I was fascinated about using it in future. I chose to use AST manipulation to implement the profiler and the trick here is to patch every function call, attribute access and other constructs. Here is what I thought: Suppose there is a function f which internally calls get_user triggering a SQL query @profile def f(request): ... user = get_user(request) ... and decorating the function f with profile decorator will manipulate its AST code. It will find all locations of function calls inside function body and will encapsulate it in our special function call_handler. So the function f's definition will becom