Skip to content

Commit e98ae0b

Browse files
committed
Update mysql.py to create index only if not exists during update
1 parent 9a3590e commit e98ae0b

File tree

1 file changed

+12
-6
lines changed
  • sdk/python/feast/infra/online_stores/contrib/mysql_online_store

1 file changed

+12
-6
lines changed

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

+12-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class MySQLOnlineStore(OnlineStore):
4141
_conn: Optional[Connection] = None
4242

4343
def _get_conn(self, config: RepoConfig) -> Connection:
44-
4544
online_store_config = config.online_store
4645
assert isinstance(online_store_config, MySQLOnlineStoreConfig)
4746

@@ -65,7 +64,6 @@ def online_write_batch(
6564
],
6665
progress: Optional[Callable[[int], Any]],
6766
) -> None:
68-
6967
conn = self._get_conn(config)
7068
cur = conn.cursor()
7169

@@ -178,18 +176,26 @@ def update(
178176

179177
# We don't create any special state for the entities in this implementation.
180178
for table in tables_to_keep:
179+
table_name = _table_id(project, table)
180+
index_name = f"{table_name}_ek"
181181
cur.execute(
182-
f"""CREATE TABLE IF NOT EXISTS {_table_id(project, table)} (entity_key VARCHAR(512),
182+
f"""CREATE TABLE IF NOT EXISTS {table_name} (entity_key VARCHAR(512),
183183
feature_name VARCHAR(256),
184184
value BLOB,
185185
event_ts timestamp NULL DEFAULT NULL,
186186
created_ts timestamp NULL DEFAULT NULL,
187187
PRIMARY KEY(entity_key, feature_name))"""
188188
)
189-
190-
cur.execute(
191-
f"ALTER TABLE {_table_id(project, table)} ADD INDEX {_table_id(project, table)}_ek (entity_key);"
189+
index_exists = cur.execute(
190+
f"""
191+
SELECT 1 FROM information_schema.statistics
192+
WHERE table_schema = DATABASE() AND table_name = '{table_name}' AND index_name = '{index_name}'
193+
"""
192194
)
195+
if not index_exists:
196+
cur.execute(
197+
f"ALTER TABLE {table_name} ADD INDEX {index_name} (entity_key);"
198+
)
193199

194200
for table in tables_to_delete:
195201
_drop_table_and_index(cur, project, table)

0 commit comments

Comments
 (0)