Skip to content

Commit 8e851d9

Browse files
gee-senbongzseta
authored andcommitted
fix: Create index only if not exists during MySQL online store update (feast-dev#3905)
Update mysql.py to create index only if not exists during update Signed-off-by: senbong <senbong@featurebyte.com> Signed-off-by: Attila Toth <hello@attilatoth.dev>
1 parent 38157f6 commit 8e851d9

File tree

1 file changed

+13
-3
lines changed
  • sdk/python/feast/infra/online_stores/contrib/mysql_online_store

1 file changed

+13
-3
lines changed

sdk/python/feast/infra/online_stores/contrib/mysql_online_store/mysql.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,28 @@ def update(
178178

179179
# We don't create any special state for the entities in this implementation.
180180
for table in tables_to_keep:
181+
182+
table_name = _table_id(project, table)
183+
index_name = f"{table_name}_ek"
181184
cur.execute(
182-
f"""CREATE TABLE IF NOT EXISTS {_table_id(project, table)} (entity_key VARCHAR(512),
185+
f"""CREATE TABLE IF NOT EXISTS {table_name} (entity_key VARCHAR(512),
183186
feature_name VARCHAR(256),
184187
value BLOB,
185188
event_ts timestamp NULL DEFAULT NULL,
186189
created_ts timestamp NULL DEFAULT NULL,
187190
PRIMARY KEY(entity_key, feature_name))"""
188191
)
189192

190-
cur.execute(
191-
f"ALTER TABLE {_table_id(project, table)} ADD INDEX {_table_id(project, table)}_ek (entity_key);"
193+
index_exists = cur.execute(
194+
f"""
195+
SELECT 1 FROM information_schema.statistics
196+
WHERE table_schema = DATABASE() AND table_name = '{table_name}' AND index_name = '{index_name}'
197+
"""
192198
)
199+
if not index_exists:
200+
cur.execute(
201+
f"ALTER TABLE {table_name} ADD INDEX {index_name} (entity_key);"
202+
)
193203

194204
for table in tables_to_delete:
195205
_drop_table_and_index(cur, project, table)

0 commit comments

Comments
 (0)