Java 获取另一个表中使用最少的id
我有两张桌子 账表Java 获取另一个表中使用最少的id,java,sql,postgresql,Java,Sql,Postgresql,我有两张桌子 账表 id INT, username TEXT, password TEXT, proxy_id INT, enabled BOOLEAN 1 10.0.0.1 4000 2 10.0.0.1 4001 代理表 id INT, proxy_ip TEXT, proxy_port INT 我会有一个循环,每次遍历一个帐户。我希望将使用最少的代理分配给table.proxy\u id 例如,如果代理表中有2个代理,帐户表中有5个帐户 id INT, username TEX
id INT, username TEXT, password TEXT, proxy_id INT, enabled BOOLEAN
1 10.0.0.1 4000
2 10.0.0.1 4001
代理表
id INT, proxy_ip TEXT, proxy_port INT
我会有一个循环,每次遍历一个帐户。我希望将使用最少的代理分配给table.proxy\u id
例如,如果代理表中有2个代理,帐户表中有5个帐户
id INT, username TEXT, password TEXT, proxy_id INT, enabled BOOLEAN
1 10.0.0.1 4000
2 10.0.0.1 4001
我们的账户
1 david password 2 enabled
2 mark password 1 enabled
3 jessica password 1 enabled
4 ashley password NULL enabled
5 allan password NULL enabled
6 james password 2 disabled
我的程序将在Java中循环,遍历所有启用的帐户,它将为该帐户分配启用帐户中使用最少的代理。在上面的示例中,David、mark和Jessica已经有了一个代理集。因此,循环将通过Ashley,id为2的代理需要分配给Ashley,因为它使用最少。对于Allan,可以指定代理1或2,因为它在任何情况下使用最少。应忽略James,因为他的帐户未启用
我希望我的问题是清楚的。我认为这需要在两个查询中完成?这里有一个想法。您需要展开代理ID以获取分配所需的列表。下面是实现这一点的基本方法,但并不难。为每个id分配额外的100个“行”,并从当前计数开始枚举它们。然后为作业订购这些 第一部分:
select p.id, count(a.id) as cnt,
generate_series(1, 100) + count(a.id) as proxy_seqnum
from proxy p left join
accounts a
on p.id = a.proxy_id and a.enabled
group by p.id;
现在,计算整体顺序:
select ap.*, row_number() over (order by proxy_seqnum) as seqnum
from (select p.id, count(a.id) as cnt,
generate_series(1, 100) + count(a.id) as proxy_seqnum
from proxy p left join
accounts a
on p.id = a.proxy_id and a.enabled
group by p.id
) ap;
我们可以使用这个顺序来匹配帐户的顺序。这将按照代理ID的“稀有性”顺序给出代理ID,从而得到更平衡的最终结果
为此,我们需要计算账户的seqnum:
update accounts a
set proxy_id = p.id;
from (select a.*, row_number() over (order by id) as seqnum
from accounts
) aa join
(select ap.*, row_number() over (order by proxy_seqnum) as seqnum
from (select p.id, count(a.id) as cnt,
generate_series(1, 100) + count(a.id) as proxy_seqnum
from proxy p left join
accounts a
on p.id = a.proxy_id and a.enabled
group by p.id
) ap
) p
on a.seqnum = p.seqnum
where a.enabled and a.proxy_id is null and
aa.id = a.id;
这里有一个想法。您需要展开代理ID以获取分配所需的列表。下面是实现这一点的基本方法,但并不难。为每个id分配额外的100个“行”,并从当前计数开始枚举它们。然后为作业订购这些 第一部分:
select p.id, count(a.id) as cnt,
generate_series(1, 100) + count(a.id) as proxy_seqnum
from proxy p left join
accounts a
on p.id = a.proxy_id and a.enabled
group by p.id;
现在,计算整体顺序:
select ap.*, row_number() over (order by proxy_seqnum) as seqnum
from (select p.id, count(a.id) as cnt,
generate_series(1, 100) + count(a.id) as proxy_seqnum
from proxy p left join
accounts a
on p.id = a.proxy_id and a.enabled
group by p.id
) ap;
我们可以使用这个顺序来匹配帐户的顺序。这将按照代理ID的“稀有性”顺序给出代理ID,从而得到更平衡的最终结果
为此,我们需要计算账户的seqnum:
update accounts a
set proxy_id = p.id;
from (select a.*, row_number() over (order by id) as seqnum
from accounts
) aa join
(select ap.*, row_number() over (order by proxy_seqnum) as seqnum
from (select p.id, count(a.id) as cnt,
generate_series(1, 100) + count(a.id) as proxy_seqnum
from proxy p left join
accounts a
on p.id = a.proxy_id and a.enabled
group by p.id
) ap
) p
on a.seqnum = p.seqnum
where a.enabled and a.proxy_id is null and
aa.id = a.id;
为此,我使用了Java和SQL。但这使它更容易阅读。我已经测试过了,它是有效的
public void setLeastUsedProxy() throws Exception {
Database db = new Database();
ArrayList<Integer> allProxies = db.getAllProxies();
ArrayList<Integer> allAssignedProxies = db.getAssignedProxies();
ArrayList<Integer> unusedProxies = allProxies;
unusedProxies.removeAll(allAssignedProxies);
// assign the unused proxy
if (unusedProxies.size() > 0) {
} else {
Integer leastUsedProxy = db.getLeastUsedProxy();
System.out.println(leastUsedProxy);
}
}
public void setLeastUsedProxy()引发异常{
数据库db=新数据库();
ArrayList allProxies=db.getAllProxies();
ArrayList allAssignedProxies=db.getAssignedProxies();
ArrayList unusedProxies=所有代理;
未使用的代理。移除所有(allAssignedProxies);
//分配未使用的代理
如果(unusedProxies.size()>0){
}否则{
整数leastUsedProxy=db.getLeastUsedProxy();
System.out.println(租赁代理);
}
}
下面是您的SQL方法
public ArrayList<Integer> getAssignedProxies() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn
.prepareStatement("select distinct(proxy_id) from account where proxy_id IS NOT null AND enabled = true");
rs = pst.executeQuery();
while (rs.next()) {
list.add(rs.getInt("proxy_id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public ArrayList<Integer> getAllProxies() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn.prepareStatement("select distinct(id) from proxy");
rs = pst.executeQuery();
while (rs.next()) {
list.add(rs.getInt("id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public Integer getLeastUsedProxy() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
Integer leastUsedProxy = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn
.prepareStatement("SELECT proxy_id, count(proxy_id) FROM account GROUP by proxy_id ORDER BY count LIMIT 1");
rs = pst.executeQuery();
while (rs.next()) {
leastUsedProxy = rs.getInt("proxy_id");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return leastUsedProxy;
}
public ArrayList getAssignedProxies()引发异常{
连接conn=null;
PreparedStatement pst=null;
结果集rs=null;
ArrayList=新建ArrayList();
试一试{
Class.forName(“org.postgresql.Driver”);
conn=DriverManager.getConnection(url、道具);
pst=conn
.prepareStatement(“从代理id不为null且已启用=true的帐户中选择distinct(代理id));
rs=pst.executeQuery();
while(rs.next()){
添加(rs.getInt(“proxy_id”);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(pst!=null){
pst.close();
}
如果(rs!=null){
rs.close();
}
如果(conn!=null){
康涅狄格州关闭();
}
}
退货清单;
}
公共ArrayList getAllProxies()引发异常{
连接conn=null;
PreparedStatement pst=null;
结果集rs=null;
ArrayList=新建ArrayList();
试一试{
Class.forName(“org.postgresql.Driver”);
conn=DriverManager.getConnection(url、道具);
pst=conn.preparest陈述(“从代理中选择不同的(id”);
rs=pst.executeQuery();
while(rs.next()){
添加(rs.getInt(“id”);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(pst!=null){
pst.close();
}
如果(rs!=null){
rs.close();
}
如果(conn!=null){
康涅狄格州关闭();
}
}
退货清单;
}
公共整数getLeastUsedProxy()引发异常{
连接conn=null;
PreparedStatement pst=null;
结果集rs=null;
整数leastUsedProxy=null;
试一试{
Class.forName(“org.postgresql.Driver”);
conn=DriverManager.getConnection(url、道具);
pst=conn
.prepareStatement(“按代理id顺序按计数限制1从帐户组中选择代理id、计数(代理id));
rs=pst.executeQuery();
while(rs.next()){
leastUsedProxy=rs.getInt(“代理id”);
}
}捕获(例外e){
e、 printStackTrace();
}最后{
如果(pst!=null){
pst.close();
}
如果(rs!=null){
rs.close();
}
如果(conn!=null){
康涅狄格州关闭();
}
}
归还租赁代理;
}
为此,我使用了Java和SQL。但这使它更容易阅读。我已经测试过了,它是有效的
public void setLeastUsedProxy() throws Exception {
Database db = new Database();
ArrayList<Integer> allProxies = db.getAllProxies();
ArrayList<Integer> allAssignedProxies = db.getAssignedProxies();
ArrayList<Integer> unusedProxies = allProxies;
unusedProxies.removeAll(allAssignedProxies);
// assign the unused proxy
if (unusedProxies.size() > 0) {
} else {
Integer leastUsedProxy = db.getLeastUsedProxy();
System.out.println(leastUsedProxy);
}
}
public void setLeastUsedProxy()引发异常{
数据库db=新数据库();
ArrayList allProxies=db.getAllProxies();
ArrayList allAssignedProxies=db.getAssignedProxies();
ArrayList unusedProxies=所有代理;
未使用的代理。移除所有(allAssignedProxies);
//分配未使用的代理
如果(unusedProxies.size()>0){
}否则{
整数leastUsedProxy=db.getLeastUsedProxy();
System.out.println(租赁代理);
}
}
下面是您的SQL方法
public ArrayList<Integer> getAssignedProxies() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn
.prepareStatement("select distinct(proxy_id) from account where proxy_id IS NOT null AND enabled = true");
rs = pst.executeQuery();
while (rs.next()) {
list.add(rs.getInt("proxy_id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public ArrayList<Integer> getAllProxies() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
ArrayList<Integer> list = new ArrayList<Integer>();
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn.prepareStatement("select distinct(id) from proxy");
rs = pst.executeQuery();
while (rs.next()) {
list.add(rs.getInt("id"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public Integer getLeastUsedProxy() throws Exception {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
Integer leastUsedProxy = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, props);
pst = conn
.prepareStatement("SELECT proxy_id, count(proxy_id) FROM account GROUP by proxy_id ORDER BY count LIMIT 1");
rs = pst.executeQuery();
while (rs.next()) {
leastUsedProxy = rs.getInt("proxy_id");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pst != null) {
pst.close();
}
if (rs != null) {
rs.close();
}
if (conn != null) {
conn.close();
}
}
return leastUsedProxy;
}
public ArrayList getAssignedProxies()引发异常{
连接conn=null;
PreparedStatement pst=null;
结果集rs=null;
ArrayList