There's a simple text field and as a user types, an api call will be made that will fetch name of people based on what user types. For example, when a user presses a button 'A', immediately an API request will be made and all names starting with 'A' will be pulled from the database. The problem currently is that since we have around 1 million entries, it is taking around 8-10 seconds for every request. Is there a way to make this faster?
Here is the axios post request that I'm making.
fetchingDataOnChange = () => {
let nameValue = this.state.searchName;
var myParams = {
nameValue: nameValue
}
axios.post('/get-hcp-data', myParams)
.then((res) => {
const hcps = res.data;
this.setState({ hcps: hcps, hcpName: Object.values(hcps.hcp_details_concat) })
console.log(res);
}, (error) => {
console.log(error);
});
}
And here is the function in flask:
@app.route('/get-hcp-data', methods=['GET', 'POST'])
def get_hcp_data():
value = request.get_json()
newValue = value['nameValue']
engine = connect_pg()
sql = "select * from dim_hcp_details where lower(hcp_name) like lower('"+newValue+"%') limit 50"
hcp_details = pd.read_sql_query(sql,engine)
hcp_details['hcp_details_concat'] = "[" + hcp_details['hcp_id'] + "]" + hcp_details['hcp_name']
hcp_dict = hcp_details.to_dict()
return jsonify(hcp_dict)