数据仓库中标记的维度

| 在我的数据仓库中,我有一个维度,每个记录可以有一个或几个标签。 有没有直接的方法可以为具有不同标签的维度建模? 我最初的想法是存储逗号分隔的列表,然后使用MySQL的FIND_IN_SET()函数测试标签的存在。这使我可以切片数据。 但是,如果我希望能够按标签分组,应该如何为我的方案建模? 例: 两个产品:产品A和产品B。A用\“ foo \”,\“ bar \”标记。 B用\“ bar \”,\“ baz \”标记。 查询:销售金额,按产品标签分组。标签“ bar”的组必须包括产品A和B的销售额:
foo -> sales for A
bar -> sales for B and A
baz -> sales for C
    
已邀请:
例如,您可以存储按标签重复的数据。 如果您的销售额为a = 10 foo + bar和b = 20 bar + baz,则可以这样存储。
    sales_by_tag_facts
    id, tag, sale_id, amount, is_primary_record
    1   foo  A        10     true
    2   bar  A        10     false
    3   bar  B        20     true
    4   baz  B        20     false

select sum(amount) from sales_by_tag_facts group by tag; // by tag
select sum(amount) from sales_by_tag_facts where is_primary_record=true; // without tag.
    
为什么不将繁重的工作从报告转移到交易预订 您可以添加一个新表,名为: TagTotal存储每个标签的总量,并通过BEFORE(AFTER)_UPDATE_EACH触发器随每个事务更新。 额外栏位/表格 如果产品表中有2个额外字段:
product.amount    decimal(10,2) running total of sales to date
product.last_sale date          date of the last sale
标签总计表如下所示。
tag.id primary autoinc    
tag.tagstr varchar(25)     
tag.amount decimal(10,2)
tag.date_from date   #dates to keep the running totals per month/week/day.
tag.date_to   date
伪码
CREATE TRIGGER ai_sales_each AFTER INSERT ON sales FOR EACH ROW
BEGIN
  UPDATE product SET product.amount = product.amount + new.amount,
    product.last_sale = sale.date
END

CREATE TRIGGER au_product_each AFTER UPDATE ON product FOR EACH ROW
BEGIN
  DECLARE AllTags VARCHAR(255);
  DECLARE ThisTag VARCHAR(25);

  IF old.tags <> new.tags THEN BEGIN
    reorganize the running totals in the tagtotal table.
  END; END IF;

  SET AllTags = new.tags;
  WHILE AllTags has more tags BEGIN
    SET ThisTag = NextTag(AllTags);
    UPDATE TagTotals SET amount = amount + new.amount
      WHERE TagTotals.tagstr = ThisTag 
      AND new.last_date BETWEEN TagTotals.date_from AND TagTotals.date_to;
  END; END WHILE; 
现在,如果要每个标签的销售总额,则只需从标签总数表中进行选择。 这将立即产生结果。     
我建议不要这样做,因为它违反了规范化规则。 看到我不断弄乱1NF 或阅读标准化标签下的帖子。 重新设计表格建议 如果您像这样制作标记和标记链接表。
table tag
  id autoincrement integer primary index 
  tag_str varchar(20) index unique not null

table taglink
  id autoincrement integer primary index #gotta have an ID in my book.
  tag_id integer not null
  product_id integer not null
并且您有一个类似这样的销售表。
table product
  id autoincement integer primary index
  desc varchar(255)
  barcode, price, whatever ...
选择语句以按标签查找产品 比起您,您可以按以下方式查找与标签匹配的文章。
select * from product
inner join taglink on (product.id = taglink.product_id)
inner join tag on (taglink.tag_id = tag.id)
where tag.tag_str in (\'foo\',\'bar\',\'baz\');
选择语句以列出每个产品的标签
select tag_str from tag
inner join taglink on (taglink.tag_id = tag.id)
inner join product on (taglink.product_id = product.id)
where product.barcode = \'4548215\' or product.desc like \'OMG Po%\'
添加新标签 要添加新标签,只需
insert into tag (id, tag_str) values (
  null /*remember autoincrement*/
  ,\'Mytag\');
链接标签 将标签链接到产品
set @product_id = 10;
set @tag_id = 1;
...or...
select @product_id:= product.id from product where product.barcode = \'1254851\';
...
insert into taglink (id, product_id, tag_id) values (
  null /*autoinc id*/
  ,@product_id
  ,@tag_id );
您可以将无限数量的标签链接到产品,并且不会因昂贵的
FIND_IN_SET
语句而减慢查询速度。 而且可以防止重复的标签。 而且您的数据库将更快,更小。     

要回复问题请先登录注册