请。我有两张最常见的名字和姓氏的表。每个表基本上有两个字段:
表
CREATE TABLE "common_first_name" (
"first_name" text PRIMARY KEY, --The text representing the name
"ratio" numeric NOT NULL, -- the % of how many times it occurs compared to the other names.
"inserted_at" timestamp WITH time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
"updated_at" timestamp WITH time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
CREATE TABLE "common_last_name" (
"last_name" text PRIMARY KEY, --The text representing the name
"ratio" numeric NOT NULL, -- the % of how many times it occurs compared to the other names.
"inserted_at" timestamp WITH time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
"updated_at" timestamp WITH time zone DEFAULT timezone('utc'::text, now()) NOT NULL
);
PS:TOP 1 的名字出现的几率只有 ~ 1.8%。这些表每个有 1000 行。
函数(伪,未就绪)
CREATE OR REPLACE FUNCTION create_sample_data(p_number_of_records INT)
RETURNS VOID
AS $$
DECLARE
SUM_OF_WEIGHTS CONSTANT INT := 100;
BEGIN
FOR i IN 1..coalesce(p_number_of_records, 0) LOOP
--Get the random first and last name but taking in consideration their probability (RATIO)round(random()*SUM_OF_WEIGHTS);
--create_person (random_first_name || ' ' || random_last_name);
END LOOP;
END
$$
LANGUAGE plpgsql VOLATILE;
PS:每个名称(每个表)的所有比率总和为 100%。
我想运行一个函数 N 次并获取一个名字和一个姓氏来创建示例数据......两个表每个都有 1000 行。
样本量可以是从 1000 个全名到 1000000 个名字的任何地方,所以如果有一种“快速”的方法来执行这个随机加权函数,那就更好了。
关于如何在 PL/PGSQL 中执行此操作的任何建议?
我在 SUPABASE.IO 上使用 PG 13.3。
谢谢