'전체 글'에 해당되는 글 1026건

  1. 2024.03.07 [SQL] BEGIN TRANSACTION
  2. 2024.02.17 Cardioid 그리기
Coding/Python 삽질기2024. 3. 7. 00:45

Python으로 작성한 SQL문에서 대량의 insert를 수행시 느려지는 문제가 있어서, 

DB전문가에게 문의를 하니, 간단히 답을 알려 주셨다.

해결책은 "BEGIN TRANSACTION"을 사용하면 되는 것이었다.

이 한줄을 넣고 나니, 10배 이상 속도가 빨라졌다;;

[기존코드]

    def insert(self, data):
        insert_memo_sql = '''INSERT INTO minim(title, memo) VALUES(?, ?);'''
        try:
            cur = self.db_conn.cursor()
            cur.execute(insert_memo_sql, (data[0], data[1]))
            self.db_conn.commit()
            print(cur.lastrowid)
            return cur.lastrowid
        except sqlite3.Error as e:
            print(e)
        return -1

[개선코드]

    def insert_bigdata(self, big_data):
        insert_memo_sql = '''INSERT INTO minim(title, memo) VALUES(?, ?);'''
        try:
            cur = self.db_conn.cursor()
            cur.execute("BEGIN TRANSACTION")
            for data in big_data:
                cur.execute(insert_memo_sql, (data[0], data[1]))
            self.db_conn.commit()
            print(cur.lastrowid)
            return cur.lastrowid
        except sqlite3.Error as e:
            print(e)
            self.db_conn.rollback()  # rollback the transaction if there's any error
        return -1

 

'Coding > Python 삽질기' 카테고리의 다른 글

Cardioid 그리기  (0) 2024.02.17
[Python] PNG to ICO 변환 하기  (0) 2024.01.04
[Python] 프랙탈 나무 만들기  (0) 2023.09.15
Posted by chobocho
Coding/Python 삽질기2024. 2. 17. 19:16
import math
import pygame


def init_game():
    pygame.init()
    pygame.key.set_repeat(0)
    canvas = pygame.display.set_mode((800, 600))
    fps_clock = pygame.time.Clock()
    return canvas, fps_clock


def main():
    canvas, fps_clock = init_game()

    title = 'Cardioid'
    pygame.display.set_caption(title)
    max_dots = 300

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return

        draw_cardioid(canvas, max_dots)
        pygame.display.update()
        fps_clock.tick(30)


def draw_cardioid(canvas, max_dots):

    canvas.fill((0, 0, 0))
    start_x = canvas.get_width() // 2
    start_y = canvas.get_height() // 2
    radius = 250

    for i in range(max_dots):
        radian = (math.pi / 180) * (360 / max_dots) * i
        sx = start_x + int(math.cos(radian) * radius)
        sy = start_y + int(math.sin(radian) * radius)

        ex = start_x + int(math.cos(radian * 2) * radius)
        ey = start_y + int(math.sin(radian * 2) * radius)

        pygame.draw.line(canvas, (0, 255, 0), (sx, sy), (ex, ey))


if __name__ == '__main__':
    main()

100개의 점을 사용한 경우
200개의 점을 사용한 경우
300개의 점을 사용한 경우

'Coding > Python 삽질기' 카테고리의 다른 글

[SQL] BEGIN TRANSACTION  (0) 2024.03.07
[Python] PNG to ICO 변환 하기  (0) 2024.01.04
[Python] 프랙탈 나무 만들기  (0) 2023.09.15
Posted by chobocho