假设我有节点use tempdb;
go
-- Test data
create table #test_data
(ItemCode char(8) not null);
insert into #test_data
values
('097-1234'),
('097-1243'),
('097-7890'),
('012-1234'),
('912-1234'),
('123-1234'), -- second max for '987,123'
('123-1234'),
('123-0001'),
('123-0932'),
('987-1234'),
('987-5643'),
('987-7890'), -- first max for '987,123'
('000-7890');
go
-- Test data
-- Code
create proc dbo.top_n_from_m
@criterias varchar(max)
as
set nocount on;
declare @crs table
(id int not null identity (1, 1) primary key,
string char(3) not null);
insert into @crs (string)
select value
from string_split(@criterias, ',')
select t.ItemCode
from
(select t.ItemCode,
c.id,
row_id = row_number() over (partition by c.id order by t.ItemCode desc)
from #test_data as t
join @crs as c on t.ItemCode like c.string + '-%') as t
where t.row_id = 1
order by t.id
go
-- Code
-- Test
execute dbo.top_n_from_m @criterias = '987,123'
select ItemCode
from #test_data
order by ItemCode
-- Test
-- Clear
drop table #test_data;
drop proc dbo.top_n_from_m;
-- Clear
,user
和item
用于加入它们。
通常会user_items
使用这样的结构:
(as advised in official documents and videos)
我想改用以下结构:
"user_items": {
"$userKey": {
"$itemKey1": true,
"$itemKey2": true,
"$itemKey3": true
}
}
值为时间戳值。所以我可以在创建日期之前订购它们,同时能够告诉相关时间。似乎像一石一样杀死两只鸟。或者是吗?
这种做法有任何不利因素吗?
编辑:我也使用这种方法来处理布尔字段,例如: "user_items": {
"$userKey": {
"$itemKey1": 1494912826601,
"$itemKey2": 1494912826602,
"$itemKey3": 1494912826603
}
}
,approved_at
,...等,而不是使用两个字段,如:
seen_at
答案 0 :(得分:1)
You can model your database in every way you want, as long as you follow Firebase rules. The most important rule is to have the data as flatten as possible. According to this rule your database is structured correctly. There is no 100% solution to have a perfect database but according to your needs and using one of the following situations, you can consider that is a good practice to do it.
1. "$itemKey1": true,
2. "$itemName1": true,
3. "$itemKey1": 1494912826601,
4. "$itemName1": 1494912826601,
What is the meaning of "$itemKey1": 1494912826601,
? Beacause you already have set a timestamp
, means that your item was uploaded into your database and is linked to the specific user, which means also in other words true
. So is not a bad approach to do something like this.
Hope it helps.
答案 1 :(得分:1)
Great minds must think alike, because I do the exact same thing :) In my case, the "items" are posts that the user has upvoted. I use the timestamps with orderBy()
, along with limitToLast(50)
to get the "last 50 posts that the user has upvoted". And from there they can load more. I see no downsides to doing this.