Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import string
- from django.db import connection,transaction
- cursor = connection.cursor()
- s = """insert into main_contact(name, number, uuid, country_code, timestamp, like_count, visible)
- (
- select a.name, a.number, a.uuid, a.country_code, now() as timestamp, 0 as like_count, true as visible
- from (
- select *
- from unnest(
- string_to_array('{}', ','),
- string_to_array('{}', ','),
- string_to_array('{}', ','),
- string_to_array('{}', ',')
- ) WITH ORDINALITY
- AS t(name, number, uuid, country_code)
- ) a
- left join main_contact b on a.number = b.number and a.name = b.name
- where b.number IS NULL
- );
- """
- N = 1000
- names = ','.join(''.join([random.choice(string.ascii_letters) for _ in range(10)]) for _ in range(N))
- numbers = ','.join(''.join([random.choice(string.digits) for _ in range(10)]) for _ in range(N))
- uuids = ','.join(''.join([random.choice(string.ascii_letters) for _ in range(10)]) for _ in range(N))
- countries = ','.join(random.choice(['KZ', 'RU']) for _ in range(N))
- def a(names, numbers, uuids, countries):
- t1 = time.time()
- s2 = s.format(names, numbers, uuids, countries)
- cursor.execute(s2)
- transaction.commit()
- return time.time() - t1
- a(names, numbers, uuids, countries)
- def add_contacts_async(contacts, uuid, country_code):
- from main.models import Contact
- numbers = [x['number'] for x in contacts]
- data = list(Contact.objects.filter(number__in=numbers).values('name', 'number'))
- new_contacts = list()
- for c in contacts:
- if {'name': c['name'], 'number': c['number']} not in data:
- new_contacts.append(Contact(name=c['name'], number=c['number'],
- uuid=uuid,
- country_code=country_code))
- Contact.objects.bulk_create(new_contacts)
- cc = [{'name': ''.join([random.choice(string.ascii_letters) for _ in range(10)]), 'number': ''.join([random.choice(string.digits) for _ in range(10)])} for _ in range(N)]
- def b():
- t1 = time.time()
- add_contacts_async(cc, 'asd', 'KZ')
- return time.time() - t1
- b()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement