Coder Social home page Coder Social logo

Comments (1)

chaudum avatar chaudum commented on June 22, 2024

hi @zopyx
what's happening in your code is that every commit requests a request, resulting in 100k requests.

you can speed up the process by doing bulk inserts. this is achieved by doing multiple adds on the same session before committing.

I've adopted your example:

import time
from datetime import datetime
import sqlalchemy as sa
from sqlalchemy import func, desc
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from crate.client import connect
from crate.client.sqlalchemy.types import Object
from crate.client.exceptions import ProgrammingError

hosts = ['localhost:4200']
engine = sa.create_engine('crate://', connect_args={'servers': hosts})
Base = declarative_base()
Session = sessionmaker(engine)
session = Session()

conn = connect(hosts)
cursor = conn.cursor()
try:
    cursor.execute('''DROP TABLE foo2''')
except ProgrammingError:
    pass
cursor.execute('''CREATE TABLE foo2 (user_id string primary key, created_at timestamp)''')


class Foo2(Base):
    __tablename__ = 'foo2'
    user_id = sa.Column(sa.String, primary_key=True)
    created_at = sa.Column(sa.DateTime)

    def __repr__(self):
        return '<Tweet {0}>'.format(self.user_id)

ts = time.time()

for i in range(100000):
    f = Foo2(user_id=i, created_at=datetime.now())
    session.add(f)
    if i % 10000 == 0:
        print(i)
    if i % 1000 == 0:
        session.commit()

print time.time() - ts
print 'done'

This one is approx twice as fast (65.69s) as your original example (125.51s).
You can still play around how many records you insert per bulk. Also note that the number of configured replicas and shards do have impact on insert times.

If you want even faster import times you should consider using the Python client directly.

e.g.

cursor.execute("""INSERT INTO foo2 (user_id, created_at) VALUES (?,?), (?, ?) ...""", [...])

see docs: https://crate.io/docs/projects/crate-python/en/latest/client.html#inserting-data

Hope this helps!
Christian

from crate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.