places that need to be optimized
sgreene570 opened this issue · comments
https://github.com/sgreene570/audiophiler/blob/master/audiophiler/__init__.py#L121
no for loop needed. We can just use sqlalchemy to do an in place query.
Same thing here
https://github.com/sgreene570/audiophiler/blob/master/audiophiler/__init__.py#L239
@sgreene570 those links are leading to bendigo. What are the new line numbers if they still need to be optimised?
@mfrancis95
https://github.com/sgreene570/audiophiler/blob/master/audiophiler/__init__.py#L116
&
https://github.com/sgreene570/audiophiler/blob/master/audiophiler/__init__.py#L226
I think. Basically any of the for loops for the DB calls could use some work.
I believe the entirety of the second link's function body could be replaced with:
return Harold.query.filter_by(owner=uid).all()
do it up
Prototyping my change for L211:
# This is a post route since auth_key is required
@app.route("/get_harold/<string:uid>", methods=["POST"])
def get_harold(uid, auth_dict=None):
data_dict = request.get_json()
if data_dict["auth_key"]:
auth_model = Auth.query.filter_by(auth_key=data_dict["auth_key"]).first()
if auth_model:
harold_file_hash = None
harolds_list = get_harold_list(uid)
if len(harolds_list) == 0:
harold_file_hash = get_random_harold()
else:
harold_file_hash = random.choice(harolds_list)
return get_file_s3(s3_bucket, harold_file_hash)
return "Permission denied", 403
And for L116:
# Check if file hash is the same as any files already in the db
if File.query.filter_by(file_hash=file_hash).first():
upload_status["error"].append(filename)
break
Regarding database optimisations that can be done to speed up the queries, @sgreene570 double check that you have indexes on:
- The
author
column in theFile
table - The
file_hash
column in theFile
table - The
file_hash
column in theHarold
table - The
owner
column in theHarold
table
done
Nice. Basically any case where you're filtering by a column, there should be an index on that column.