GraphQl查询执行时间过长(无法一次执行)

时间:2019-07-02 19:33:03

标签: sql-server graphql

我正在将REST更改为GraphQL,几乎所有工作都在进行,但是查询执行时间过长。我的查询或表中的关联有问题。当查询正在执行时,我在控制台中看到它像在某些片段中执行了很多次,并且需要很长时间。在REST中,只有一声叫喊,等待2秒钟才能得到响应。

应用程序在具有连接到MSSQL数据库的sequelize的节点/表达式上运行

//declaration order type
const OrderType = new GraphQLObjectType({
name: "Order",
fields: () => ({
id: { type: GraphQLID },
dateInsert: { type: GraphQLString },
signature: { type: GraphQLString },
symbol: { type: GraphQLString },
details: { type: GraphQLString },
closed: { type: GraphQLBoolean },
documentStatus: { type: GraphQLInt },
clientId: { type: GraphQLID },
traderId: { type: GraphQLID },
addressId: { type: GraphQLID },
addressOutId: { type: GraphQLID },
client: {
  type: ClientType,
  resolve(parent, args) {
    return Client.findOne({
      where: {
        id: parent.clientId
      }
    });
  }
},
address: {
  type: AddressType,
  resolve(parent, args) {
    return Address.findOne({
      where: {
        id: parent.addressId
      }
    });
  }
},
address2: {
  type: AddressType,
  resolve(parent, args) {
    return Address.findOne({
      where: {
        id: parent.addressOutId
      }
    });
  }
},
trader: {
  type: TraderType,
  resolve(parent, args) {
    return Trader.findOne({
      where: {
        id: parent.traderId
      }
    });
  }
},
items: {
  type: new GraphQLList(ItemType),
  resolve(parent, args) {
    return Item.findAll({
      where: {
        itemId: parent.id
      }
    });
  }
}

// numberOfDocumentInvoice: { type: GraphQLInt }
})
});

//my root query
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
order: {
  type: OrderType,
  args: { id: { type: GraphQLID } },
  resolve(parent, args) {
    return Order.findOne({
      where: {
        id: args.id
      }
    });
  }
},
client: {
  type: ClientType,
  args: { id: { type: GraphQLID } },
  resolve(parent, args) {
    return Client.findOne({
      where: {
        id: args.id
      }
    });
  }
},
orders: {
  type: new GraphQLList(OrderType),
  resolve(parent, args) {
    return Order.findAll({
      include: [
        { model: Client, required: true },
        { model: Address },
        { model: Trader, required: true, include: [{ model: User }] },
        {
          model: Item,
          required: true,
          include: [
            {
              model: Assortment,
              required: true,
              include: [
                { model: Kind, required: true },
                { model: Type, required: true }
              ]
            }
          ]
        }
      ],
      where: {
        symbol: {
          [Op.or]: ["ZK", "FP"]
        },
        dateInsert: { [Op.gte]: "2019-06-01" }
      },
      order: [["id", "DESC"]]
    });
  }
},
clients: {
  type: new GraphQLList(ClientType),
  resolve(parent, args) {
    return Client.findAll();
  }
}
}
});

//associations in order.js model
Order.belongsTo(Client, { foreignKey: "clientId" });
Client.hasOne(Order, { foreignKey: "clientId" });
Order.belongsTo(Trader, { foreignKey: "traderId" });
Trader.hasOne(Order, { foreignKey: "traderId" });
Order.belongsTo(Address, {
foreignKey: { [Op.or]: ["addressId", "addressOutId"] }
});
Address.hasOne(Order, {
foreignKey: { [Op.or]: ["addressId", "addressOutId"] }
});

我希望SQL查询像以前一样,并且响应时间短。

const sqlQuery = `SELECT d.id 
    ,d.DataWprowadzenia AS dateInsert
    ,podmiot.Nazwa AS client
    ,d.NumerWewnetrzny_PelnaSygnatura AS signature
    ,d.Symbol AS symbol
    ,asortyment.Symbol AS code
    ,asortyment.Nazwa AS assortment
    ,grupa.Nazwa AS type
    ,rodzaj.Symbol AS kind
    ,pozycje.Ilosc AS quantity
    ,pozycje.Cena_NettoPoRabacie AS price
    ,pozycje.Wartosc_NettoPoRabacie AS netValue
    ,d.Uwagi AS details
    ,d.Zamkniety AS closed
    ,d.StatusDokumentuId AS documentStatus
    ,adres.LiniaCalosc AS deliveryAddress
    ,uzytkownicy.Login AS trader
    ,pozycje.NumerReferencyjny AS itemId
        ,dokument_realizujacy.DokumentyRealizujace_Id AS numberOfDocumentInvoice
      FROM [Nexo_Goodmarks].[ModelDanychContainer].[Dokumenty] d 
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[AdresHistorie] adres 
      ON d.MiejsceDostawyId = adres.Id or d.MiejsceDostawyZewnetrzneId = adres.Id
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[PodmiotHistorie] podmiot 
      ON d.PodmiotWybranyId = podmiot.Id 
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[PozycjeDokumentu] pozycje 
      ON d.Id = pozycje.Dokument_Id 
      INNER JOIN (Select * FROM [Nexo_Goodmarks].[ModelDanychContainer].[Asortymenty] WHERE Symbol <> 'TRANSPORT IN POST' and Symbol <> 'TRANSPORT') asortyment
      ON pozycje.AsortymentAktualnyId = asortyment.Id
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[RodzajeAsortymentu] rodzaj
      ON asortyment.Rodzaj_Id = rodzaj.Id
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[GrupyAsortymentu] grupa
      ON asortyment.Grupa_Id = grupa.Id
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[OpiekunowiePodmiotu] opiekunowie
      ON d.PodmiotId = opiekunowie.PodmiotOpiekunaPodstawowego_Id
      INNER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[Uzytkownicy] uzytkownicy
      ON uzytkownicy.Id = opiekunowie.UzytkownikId
          LEFT OUTER JOIN [Nexo_Goodmarks].[ModelDanychContainer].[DokumentDokument] dokument_realizujacy
      ON dokument_realizujacy.[DokumentyRealizowane_Id] = d.Id
      WHERE (d.Symbol = 'ZK' or d.Symbol = 'FP') and (d.DataWprowadzenia >= '2019-06-01')
      ORDER BY d.Id DESC`;

0 个答案:

没有答案