0

我有一个函数,Appointment#serializable_hash看起来像这样:

  def serializable_hash(options={})
    options = { 
      :methods => %w( 
        notes
        client
        services
        products
        has_payments
        start_time_ymd
        start_time_time
        calculated_length
        day_of_week_index
        services_with_info
        products_with_info
        payments_with_info
        stylist_name_index
        time_block_type_code
        client_name_that_fits
        minutes_since_midnight
        generous_calculated_length
      )}.update(options)
    super(options)
  end 

当我在现有Appointment对象上调用该函数时,SQL 运行如下所示:

  Appointment Load (4.6ms)  SELECT "appointment".* FROM "appointment" ORDER BY start_time DESC LIMIT 1
  Appointment Load (0.6ms)  SELECT "appointment".* FROM "appointment" WHERE "appointment"."id" = $1 LIMIT 1  [["id", 4533]]
  Client Load (0.7ms)  SELECT "client".* FROM "client" WHERE "client"."id" = 398 LIMIT 1
  Address Load (0.5ms)  SELECT "address".* FROM "address" WHERE "address"."id" = 10 LIMIT 1
   (15.4ms)  SELECT COUNT(*) FROM "payment" WHERE "payment"."appointment_id" = 4533
  TimeBlockType Load (11.1ms)  SELECT "time_block_type".* FROM "time_block_type" WHERE "time_block_type"."id" = 2 LIMIT 1
  AppointmentService Load (30.3ms)  SELECT "appointment_service".* FROM "appointment_service" WHERE "appointment_service"."appointment_id" = 4533
  Service Load (18.7ms)  SELECT "service".* FROM "service" WHERE "service"."id" = 295 ORDER BY name LIMIT 1
  AppointmentProduct Load (6.2ms)  SELECT "appointment_product".* FROM "appointment_product" WHERE "appointment_product"."appointment_id" = 4533
  Payment Load (0.5ms)  SELECT "payment".* FROM "payment" WHERE "payment"."appointment_id" = 4533
  Stylist Load (15.5ms)  SELECT "stylist".* FROM "stylist" WHERE "stylist"."active" = 't' AND "stylist"."id" = 84 LIMIT 1
  StylistEmploymentType Load (0.7ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  Salon Load (0.7ms)  SELECT "salon".* FROM "salon" WHERE "salon"."id" = 29 LIMIT 1
  Stylist Load (1.0ms)  SELECT "stylist".* FROM "stylist" WHERE "stylist"."active" = 't' AND (salon_id = 29 AND active = true AND name != 'R.')
  StylistEmploymentType Load (0.3ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  StylistEmploymentType Load (0.3ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  StylistEmploymentType Load (0.3ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  StylistEmploymentType Load (0.3ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  StylistEmploymentType Load (0.3ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  StylistEmploymentType Load (0.2ms)  SELECT "stylist_employment_type".* FROM "stylist_employment_type" WHERE "stylist_employment_type"."code" = 'CHAIR_RENTAL' LIMIT 1
  Service Load (3.6ms)  SELECT "service".* FROM "service" INNER JOIN "appointment_service" ON "service"."id" = "appointment_service"."service_id" WHERE "appointment_service"."appointment_id" = 4533 ORDER BY name
  Product Load (16.5ms)  SELECT "product".* FROM "product" INNER JOIN "appointment_product" ON "product"."id" = "appointment_product"."product_id" WHERE "appointment_product"."appointment_id" = 4533 ORDER BY brand_id, name

显然,这比它可能的效率要低。如何serializable_hash在尽可能少的查询中运行?有没有更好的方法来做我正在做的事情,而不是参与serializable_hash

4

1 回答 1

-1

我的问题来自一个糟糕的设计,我使用RABL修复了我的糟糕设计。

于 2013-02-22T18:50:56.207 回答