improve sqlite speed when data is not kept on cache
I have a software that runs continuously and that periodically read from db. On some platform we observed that sometimes the reads were very slow and we figured out that it was due to the cache cleaning done by the operative system.
I have replicated the issue in the following script:
import subprocess
from subprocess import call
import time
import pandas as pd
import numpy as np
from sqlalchemy.orm import sessionmaker
from sqlalchemy import func, distinct, text
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, create_engine, and_
import os
n_users = 1000
n_days = 60
n_domains = 100
all_users = ['user%d' % i for i in range(n_users)]
all_domains = ['domain%d' % i for i in range(n_domains)]
n_rows = n_users*n_days*n_domains
Base = declarative_base()
#file_path = '/home/local/CORVIL/lpuggini/Desktop/example.db'
file_path = '/data/misc/luca/example.db'
db_path = 'sqlite:///' + file_path
engine = create_engine(db_path)
def get_session():
Session = sessionmaker(bind=engine)
session = Session()
Base.metadata.create_all(engine)
return session
class DailyUserWebsite(Base):
__tablename__ = 'daily_user_website'
id = Column(Integer, primary_key=True)
user = Column(String(600), index=True)
domain = Column(String(600))
time_secs = Column(Integer, index=True)
def __repr__(self):
return "DailyUserWebsite(user='%s', domain='%s', time_secs=%d)" %
(self.user, self.domain, self.time_secs)
def get_df_daily_data_per_users(users):
session = get_session()
query = session.query(DailyUserWebsite).filter(DailyUserWebsite.user.in_(users))
df = pd.read_sql(query.statement, query.session.bind)
session.close()
return df
def create_db():
if os.path.exists(file_path):
os.remove(file_path)
session = get_session()
batch_size = 10000
n_iter = int(n_rows / batch_size) + 1
for i in range(n_iter):
print 'Building db iteration %d out of %d' % (i, n_iter)
df = pd.DataFrame()
df['user'] = np.random.choice(all_users, batch_size)
df['domain'] = np.random.choice(all_domains, batch_size)
df['time_secs'] = [x - x%(3600*24) for x in np.random.randint(0, 3600*24*60, batch_size)]
df.to_sql('daily_user_website', engine, if_exists='append', index=False)
create_db()
for i in range(20):
users = np.random.choice(all_users, 200)
t0 = time.time()
df = get_df_daily_data_per_users(users)
t1 = time.time()
print 'it=', i, 'time taken to read %d rows %f ' % (df.shape[0], t1-t0)
if i % 5 == 0:
print 'Clean cache'
os.system("sync; echo 3 > /proc/sys/vm/drop_caches")
That generates the following outputs:
(samenv) probe686:/data/misc/luca # python db_test.py
it= 0 time taken to read 1089089 rows 8.058407
Clean cache
it= 1 time taken to read 1099234 rows 104.352085
it= 2 time taken to read 1087292 rows 8.189860
it= 3 time taken to read 1077284 rows 8.176948
it= 4 time taken to read 1057111 rows 7.980002
it= 5 time taken to read 1075694 rows 8.144479
Clean cache
it= 6 time taken to read 1117925 rows 106.357740
it= 7 time taken to read 1124208 rows 8.523779
it= 8 time taken to read 1083049 rows 8.368766
it= 9 time taken to read 1112264 rows 9.233548
it= 10 time taken to read 1098628 rows 8.316519
Clean cache
Is there any way to improve speed after a cache cleaning or to mitigate the effect?
python sqlite sqlalchemy
add a comment |
I have a software that runs continuously and that periodically read from db. On some platform we observed that sometimes the reads were very slow and we figured out that it was due to the cache cleaning done by the operative system.
I have replicated the issue in the following script:
import subprocess
from subprocess import call
import time
import pandas as pd
import numpy as np
from sqlalchemy.orm import sessionmaker
from sqlalchemy import func, distinct, text
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, create_engine, and_
import os
n_users = 1000
n_days = 60
n_domains = 100
all_users = ['user%d' % i for i in range(n_users)]
all_domains = ['domain%d' % i for i in range(n_domains)]
n_rows = n_users*n_days*n_domains
Base = declarative_base()
#file_path = '/home/local/CORVIL/lpuggini/Desktop/example.db'
file_path = '/data/misc/luca/example.db'
db_path = 'sqlite:///' + file_path
engine = create_engine(db_path)
def get_session():
Session = sessionmaker(bind=engine)
session = Session()
Base.metadata.create_all(engine)
return session
class DailyUserWebsite(Base):
__tablename__ = 'daily_user_website'
id = Column(Integer, primary_key=True)
user = Column(String(600), index=True)
domain = Column(String(600))
time_secs = Column(Integer, index=True)
def __repr__(self):
return "DailyUserWebsite(user='%s', domain='%s', time_secs=%d)" %
(self.user, self.domain, self.time_secs)
def get_df_daily_data_per_users(users):
session = get_session()
query = session.query(DailyUserWebsite).filter(DailyUserWebsite.user.in_(users))
df = pd.read_sql(query.statement, query.session.bind)
session.close()
return df
def create_db():
if os.path.exists(file_path):
os.remove(file_path)
session = get_session()
batch_size = 10000
n_iter = int(n_rows / batch_size) + 1
for i in range(n_iter):
print 'Building db iteration %d out of %d' % (i, n_iter)
df = pd.DataFrame()
df['user'] = np.random.choice(all_users, batch_size)
df['domain'] = np.random.choice(all_domains, batch_size)
df['time_secs'] = [x - x%(3600*24) for x in np.random.randint(0, 3600*24*60, batch_size)]
df.to_sql('daily_user_website', engine, if_exists='append', index=False)
create_db()
for i in range(20):
users = np.random.choice(all_users, 200)
t0 = time.time()
df = get_df_daily_data_per_users(users)
t1 = time.time()
print 'it=', i, 'time taken to read %d rows %f ' % (df.shape[0], t1-t0)
if i % 5 == 0:
print 'Clean cache'
os.system("sync; echo 3 > /proc/sys/vm/drop_caches")
That generates the following outputs:
(samenv) probe686:/data/misc/luca # python db_test.py
it= 0 time taken to read 1089089 rows 8.058407
Clean cache
it= 1 time taken to read 1099234 rows 104.352085
it= 2 time taken to read 1087292 rows 8.189860
it= 3 time taken to read 1077284 rows 8.176948
it= 4 time taken to read 1057111 rows 7.980002
it= 5 time taken to read 1075694 rows 8.144479
Clean cache
it= 6 time taken to read 1117925 rows 106.357740
it= 7 time taken to read 1124208 rows 8.523779
it= 8 time taken to read 1083049 rows 8.368766
it= 9 time taken to read 1112264 rows 9.233548
it= 10 time taken to read 1098628 rows 8.316519
Clean cache
Is there any way to improve speed after a cache cleaning or to mitigate the effect?
python sqlite sqlalchemy
Why are you telling your OS to drop its caches?
– Shawn
Nov 20 '18 at 17:18
I am doing to replicate what we suspect is happening in production. Our code run togheter with other code. When the other code is runned the database is removed by cache by time to tim.e
– Donbeo
Nov 20 '18 at 19:03
add a comment |
I have a software that runs continuously and that periodically read from db. On some platform we observed that sometimes the reads were very slow and we figured out that it was due to the cache cleaning done by the operative system.
I have replicated the issue in the following script:
import subprocess
from subprocess import call
import time
import pandas as pd
import numpy as np
from sqlalchemy.orm import sessionmaker
from sqlalchemy import func, distinct, text
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, create_engine, and_
import os
n_users = 1000
n_days = 60
n_domains = 100
all_users = ['user%d' % i for i in range(n_users)]
all_domains = ['domain%d' % i for i in range(n_domains)]
n_rows = n_users*n_days*n_domains
Base = declarative_base()
#file_path = '/home/local/CORVIL/lpuggini/Desktop/example.db'
file_path = '/data/misc/luca/example.db'
db_path = 'sqlite:///' + file_path
engine = create_engine(db_path)
def get_session():
Session = sessionmaker(bind=engine)
session = Session()
Base.metadata.create_all(engine)
return session
class DailyUserWebsite(Base):
__tablename__ = 'daily_user_website'
id = Column(Integer, primary_key=True)
user = Column(String(600), index=True)
domain = Column(String(600))
time_secs = Column(Integer, index=True)
def __repr__(self):
return "DailyUserWebsite(user='%s', domain='%s', time_secs=%d)" %
(self.user, self.domain, self.time_secs)
def get_df_daily_data_per_users(users):
session = get_session()
query = session.query(DailyUserWebsite).filter(DailyUserWebsite.user.in_(users))
df = pd.read_sql(query.statement, query.session.bind)
session.close()
return df
def create_db():
if os.path.exists(file_path):
os.remove(file_path)
session = get_session()
batch_size = 10000
n_iter = int(n_rows / batch_size) + 1
for i in range(n_iter):
print 'Building db iteration %d out of %d' % (i, n_iter)
df = pd.DataFrame()
df['user'] = np.random.choice(all_users, batch_size)
df['domain'] = np.random.choice(all_domains, batch_size)
df['time_secs'] = [x - x%(3600*24) for x in np.random.randint(0, 3600*24*60, batch_size)]
df.to_sql('daily_user_website', engine, if_exists='append', index=False)
create_db()
for i in range(20):
users = np.random.choice(all_users, 200)
t0 = time.time()
df = get_df_daily_data_per_users(users)
t1 = time.time()
print 'it=', i, 'time taken to read %d rows %f ' % (df.shape[0], t1-t0)
if i % 5 == 0:
print 'Clean cache'
os.system("sync; echo 3 > /proc/sys/vm/drop_caches")
That generates the following outputs:
(samenv) probe686:/data/misc/luca # python db_test.py
it= 0 time taken to read 1089089 rows 8.058407
Clean cache
it= 1 time taken to read 1099234 rows 104.352085
it= 2 time taken to read 1087292 rows 8.189860
it= 3 time taken to read 1077284 rows 8.176948
it= 4 time taken to read 1057111 rows 7.980002
it= 5 time taken to read 1075694 rows 8.144479
Clean cache
it= 6 time taken to read 1117925 rows 106.357740
it= 7 time taken to read 1124208 rows 8.523779
it= 8 time taken to read 1083049 rows 8.368766
it= 9 time taken to read 1112264 rows 9.233548
it= 10 time taken to read 1098628 rows 8.316519
Clean cache
Is there any way to improve speed after a cache cleaning or to mitigate the effect?
python sqlite sqlalchemy
I have a software that runs continuously and that periodically read from db. On some platform we observed that sometimes the reads were very slow and we figured out that it was due to the cache cleaning done by the operative system.
I have replicated the issue in the following script:
import subprocess
from subprocess import call
import time
import pandas as pd
import numpy as np
from sqlalchemy.orm import sessionmaker
from sqlalchemy import func, distinct, text
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, create_engine, and_
import os
n_users = 1000
n_days = 60
n_domains = 100
all_users = ['user%d' % i for i in range(n_users)]
all_domains = ['domain%d' % i for i in range(n_domains)]
n_rows = n_users*n_days*n_domains
Base = declarative_base()
#file_path = '/home/local/CORVIL/lpuggini/Desktop/example.db'
file_path = '/data/misc/luca/example.db'
db_path = 'sqlite:///' + file_path
engine = create_engine(db_path)
def get_session():
Session = sessionmaker(bind=engine)
session = Session()
Base.metadata.create_all(engine)
return session
class DailyUserWebsite(Base):
__tablename__ = 'daily_user_website'
id = Column(Integer, primary_key=True)
user = Column(String(600), index=True)
domain = Column(String(600))
time_secs = Column(Integer, index=True)
def __repr__(self):
return "DailyUserWebsite(user='%s', domain='%s', time_secs=%d)" %
(self.user, self.domain, self.time_secs)
def get_df_daily_data_per_users(users):
session = get_session()
query = session.query(DailyUserWebsite).filter(DailyUserWebsite.user.in_(users))
df = pd.read_sql(query.statement, query.session.bind)
session.close()
return df
def create_db():
if os.path.exists(file_path):
os.remove(file_path)
session = get_session()
batch_size = 10000
n_iter = int(n_rows / batch_size) + 1
for i in range(n_iter):
print 'Building db iteration %d out of %d' % (i, n_iter)
df = pd.DataFrame()
df['user'] = np.random.choice(all_users, batch_size)
df['domain'] = np.random.choice(all_domains, batch_size)
df['time_secs'] = [x - x%(3600*24) for x in np.random.randint(0, 3600*24*60, batch_size)]
df.to_sql('daily_user_website', engine, if_exists='append', index=False)
create_db()
for i in range(20):
users = np.random.choice(all_users, 200)
t0 = time.time()
df = get_df_daily_data_per_users(users)
t1 = time.time()
print 'it=', i, 'time taken to read %d rows %f ' % (df.shape[0], t1-t0)
if i % 5 == 0:
print 'Clean cache'
os.system("sync; echo 3 > /proc/sys/vm/drop_caches")
That generates the following outputs:
(samenv) probe686:/data/misc/luca # python db_test.py
it= 0 time taken to read 1089089 rows 8.058407
Clean cache
it= 1 time taken to read 1099234 rows 104.352085
it= 2 time taken to read 1087292 rows 8.189860
it= 3 time taken to read 1077284 rows 8.176948
it= 4 time taken to read 1057111 rows 7.980002
it= 5 time taken to read 1075694 rows 8.144479
Clean cache
it= 6 time taken to read 1117925 rows 106.357740
it= 7 time taken to read 1124208 rows 8.523779
it= 8 time taken to read 1083049 rows 8.368766
it= 9 time taken to read 1112264 rows 9.233548
it= 10 time taken to read 1098628 rows 8.316519
Clean cache
Is there any way to improve speed after a cache cleaning or to mitigate the effect?
python sqlite sqlalchemy
python sqlite sqlalchemy
edited Nov 21 '18 at 8:53
Donbeo
asked Nov 20 '18 at 15:21
DonbeoDonbeo
4,8812167119
4,8812167119
Why are you telling your OS to drop its caches?
– Shawn
Nov 20 '18 at 17:18
I am doing to replicate what we suspect is happening in production. Our code run togheter with other code. When the other code is runned the database is removed by cache by time to tim.e
– Donbeo
Nov 20 '18 at 19:03
add a comment |
Why are you telling your OS to drop its caches?
– Shawn
Nov 20 '18 at 17:18
I am doing to replicate what we suspect is happening in production. Our code run togheter with other code. When the other code is runned the database is removed by cache by time to tim.e
– Donbeo
Nov 20 '18 at 19:03
Why are you telling your OS to drop its caches?
– Shawn
Nov 20 '18 at 17:18
Why are you telling your OS to drop its caches?
– Shawn
Nov 20 '18 at 17:18
I am doing to replicate what we suspect is happening in production. Our code run togheter with other code. When the other code is runned the database is removed by cache by time to tim.e
– Donbeo
Nov 20 '18 at 19:03
I am doing to replicate what we suspect is happening in production. Our code run togheter with other code. When the other code is runned the database is removed by cache by time to tim.e
– Donbeo
Nov 20 '18 at 19:03
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396186%2fimprove-sqlite-speed-when-data-is-not-kept-on-cache%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396186%2fimprove-sqlite-speed-when-data-is-not-kept-on-cache%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why are you telling your OS to drop its caches?
– Shawn
Nov 20 '18 at 17:18
I am doing to replicate what we suspect is happening in production. Our code run togheter with other code. When the other code is runned the database is removed by cache by time to tim.e
– Donbeo
Nov 20 '18 at 19:03