我需要将数据从事务数据库转换成星型模式。我已经创建了所有维度表和一个事实表(cinjenica),如erd图片所示:左(星型模式)右(事务数据库表)
我还在维度表上创建了唯一索引,以便限制最大理论行数例如在dimenzijaèu platforma中,我在(widows、linux、mac)属性上有唯一索引,这些属性的值可以是1或非1(二进制值),这将产生2^3=8个最大理论行。对于将从具有29k行的事务数据库表派生的其他维度表也是如此。
表cinjenica(事实表)包含复合pk/fk,所有外键都具有更新级联。
现在,为了在我的星型架构中输入数据,我创建了以下存储过程:
CREATE DEFINER=`root`@`localhost` PROCEDURE `new_row_to_star_scheme`()
BEGIN
DECLARE i INTEGER;
DECLARE total_row_count INTEGER;
select appid from sppi.steam;
SELECT FOUND_ROWS() into total_row_count;
SET i = 0;
ponavljanje: LOOP
IF i > total_row_count THEN
LEAVE ponavljanje;
END IF;
INSERT INTO dimenzija_datum(ID,datum)
SELECT last_insert_id(),s.release_date
FROM steam as s LIMIT i,1
ON DUPLICATE KEY UPDATE ID=last_insert_id(ID);
SET @row_datum = last_insert_id();
INSERT INTO dimenzija_vlasnik(ID,developer,publisher)
SELECT last_insert_id(),s.developer, s.publisher
FROM steam as s LIMIT i,1
ON DUPLICATE KEY UPDATE ID=last_insert_id(ID);
SET @row_vlasnik = last_insert_id();
INSERT INTO dimenzija_platforme(ID,tekstualno)
SELECT last_insert_id(),s.platforms
FROM steam as s LIMIT i,1
ON DUPLICATE KEY UPDATE ID=last_insert_id(ID);
SET @row_platforme = last_insert_id();
INSERT INTO dimenzija_kategorija(ID,zanr_tagovi,tip_tagovi)
SELECT last_insert_id(),s.genres, s.categories
FROM steam as s LIMIT i,1
ON DUPLICATE KEY UPDATE ID=last_insert_id(ID);
SET @row_kategorija = last_insert_id();
INSERT INTO cinjenica(fk_datum,fk_vlasnik,fk_platforma,fk_kategorija,naziv,positive_rating,negative_rating,avg_playtime,median_playtime,cijena,owners)
SELECT @row_datum, @row_vlasnik, @row_platforme, @row_kategorija, s.name, s.positive_ratings, s.negative_ratings, s.average_playtime, s.median_playtime, s.price, s.owners
FROM steam as s LIMIT i,1
ON DUPLICATE KEY UPDATE ID=last_insert_id(ID);
SET i = i+1;
END LOOP;
END
未从所示过程插入到维度中的所有其他数据都是在每个维度表上使用before insert触发器创建的。
使用此存储过程,我可以填充我的星型架构,直到任何维度表遇到重复行,然后在“field list”中得到错误代码1054未知列“id”。每次插入后,如果有重复的数据-在重复键更新时,应该更新维度表的主键(事实表的外键)并在更新时触发级联,因此理论上它应该工作。我应该如何更正填充星型模式的过程?
暂无答案!
目前还没有任何答案,快来回答吧!