Now many developers (not all) have a tendency to use a number of technologies, not understanding how they work and which problems they solve and which ones they create. The talk will be devoted to a review of libraries in Python, which help us work with databases, namely, effectively receive data.
I will try to tell and answer the following questions:
why is it sometimes worth writing RAW SQL;
when it is worth paying attention to ORM, in particular, their advantages and differences;
where and how ORM will be slower, and where there will be no difference;
how and what ORMs can work in asynchronous mode;
what does GraphQL have to do with it and how can it help.
7. Raw SQL in python
import psycopg2
class pg_conn:
def __enter__(self):
self.conn = psycopg2.connect(
dbname='fwdays', user='highload'
)
return self.conn
def __exit__(self, exc_type, exc_val, exc_tb):
if hasattr(self, 'conn'):
self.conn.close()
7
with pg_conn() as db:
cur = db.cursor()
query = "insert into product('name') values('toy')"
cur.execute(query)
db.commit()
cur.close()
db.close()
8. Raw SQL in python
● psycopg2
● written in C
● implementation of the DB API 2.0 (PEP 249)
● works like a libpq wrapper
8
9. Raw SQL in python
● psycopg2
● written in C
● implementation of the DB API 2.0 (PEP 249)
● works like a libpq wrapper
9
10. Raw SQL in python
import psycopg2
class pg_conn:
def __enter__(self):
self.conn = psycopg2.connect(
dbname='fwdays', user='highload'
)
return self.conn
def __exit__(self, exc_type, exc_val, exc_tb):
if hasattr(self, 'conn'):
self.conn.close()
10
with pg_conn() as db:
cur = db.cursor()
query = "insert into product('name') values('toy')"
cur.execute(query)
db.commit()
cur.close()
db.close()
11. Raw SQL in python
insert into products('name') values('toy');
11
12. Raw SQL in python
insert into products('name') values('toy');
select settings from application_settings
where company_id in (
3244414, 3239428, 3246756, 3215974
);
12
13. Raw SQL in python
13
select csa.company_id, count(csa.id)
from company_site_attributes as csa
join company as c on c.id = csa.company_id
join site_filters as csf on csf.attribute_id = csa.id
where portal_attribute_id in (1536, 17536) and
c.premium_service_id > 0 and
csa.status = 0 and
csf.status = 0
group by csa.company_id, csa.id
having count(csa.id) > 1;
14. Raw SQL in python
14
select csa.company_id, count(csa.id)
from company_site_attributes as csa
join company as c on c.id = csa.company_id
join site_filters as csf on csf.attribute_id = csa.id
where portal_attribute_id in (1536, 17536) and
c.premium_service_id > 0 and
csa.status = 0 and
csf.status = 0
group by csa.company_id, csa.id
having count(csa.id) > 1;
17. Raw SQL
● small tables
● few tables
● no complicated types
17
18. Raw SQL
● small tables
● few tables
● no complicated types
● no need to manage scheme in the app
18
19. Raw SQL
● small tables
● few tables
● no complicated types
● no need to manage scheme in the app
● no migrations
19
20. Raw SQL
● small tables
● few tables
● no complicated types
● no need to manage scheme in the app
● no migrations
● no dynamic queries
20
21. Raw SQL
● small tables
● few tables
● no complicated types
● no need to manage scheme in the app
● no migrations
● no dynamic queries
● no app session managing
● ...
21